blob: 377a1b07b0000ea35656865244a302c8d787ba64 [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
Pau Espin Pedrol21d03d32019-07-22 12:05:52 +02004* SPDX-License-Identifier: AGPL-3.0+
dburgess82c46ff2011-10-07 02:40:51 +00005*
6* This software is distributed under the terms of the GNU Affero Public License.
7* See the COPYING file in the main directory for details.
8*
9* This use of this software may be subject to additional restrictions.
10* See the LEGAL file in the main directory for details.
11
12 This program is free software: you can redistribute it and/or modify
13 it under the terms of the GNU Affero General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Affero General Public License for more details.
21
22 You should have received a copy of the GNU Affero General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
24
25*/
26
27
Pau Espin Pedrol5b60c982018-09-20 18:04:46 +020028#include <string.h>
29#include <sys/types.h>
dburgess82c46ff2011-10-07 02:40:51 +000030
31#include "Threads.h"
32#include "Timeval.h"
Pau Espin Pedrol5b60c982018-09-20 18:04:46 +020033#include "Logger.h"
34
Pau Espin Pedrol6c646c32021-03-01 16:35:35 +010035extern "C" {
Pau Espin Pedrol57389402021-02-17 18:25:45 +010036#include <osmocom/core/thread.h>
Pau Espin Pedrol6c646c32021-03-01 16:35:35 +010037}
Pau Espin Pedrol57389402021-02-17 18:25:45 +010038
dburgess82c46ff2011-10-07 02:40:51 +000039using namespace std;
40
Pau Espin Pedrole503c982019-09-13 18:56:08 +020041#ifndef HAVE_ATOMIC_OPS
42 pthread_mutex_t atomic_ops_mutex = PTHREAD_MUTEX_INITIALIZER;
43#endif
dburgess82c46ff2011-10-07 02:40:51 +000044
45
dburgess82c46ff2011-10-07 02:40:51 +000046
Pau Espin Pedrol5b60c982018-09-20 18:04:46 +020047void set_selfthread_name(const char *name)
48{
49 pthread_t selfid = pthread_self();
Pau Espin Pedrol57389402021-02-17 18:25:45 +010050 pid_t tid = osmo_gettid();
Pau Espin Pedrol5b60c982018-09-20 18:04:46 +020051 if (pthread_setname_np(selfid, name) == 0) {
52 LOG(INFO) << "Thread "<< selfid << " (task " << tid << ") set name: " << name;
53 } else {
54 char buf[256];
55 int err = errno;
56 char* err_str = strerror_r(err, buf, sizeof(buf));
57 LOG(NOTICE) << "Thread "<< selfid << " (task " << tid << ") set name \"" << name << "\" failed: (" << err << ") " << err_str;
58 }
59}
dburgess82c46ff2011-10-07 02:40:51 +000060
Pau Espin Pedrol75cb0b92019-04-25 19:33:58 +020061void thread_enable_cancel(bool cancel)
62{
63 cancel ? pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) :
64 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
65}
66
dburgess82c46ff2011-10-07 02:40:51 +000067void Thread::start(void *(*task)(void*), void *arg)
68{
69 assert(mThread==((pthread_t)0));
70 bool res;
kurtis.heimerl5a872472013-05-31 21:47:25 +000071 // (pat) Moved initialization to constructor to avoid crash in destructor.
72 //res = pthread_attr_init(&mAttrib);
73 //assert(!res);
Eric Wildac0487e2019-06-17 13:02:44 +020074 if (mStackSize != 0) {
75 res = pthread_attr_setstacksize(&mAttrib, mStackSize);
76 assert(!res);
77 }
dburgess82c46ff2011-10-07 02:40:51 +000078 res = pthread_create(&mThread, &mAttrib, task, arg);
79 assert(!res);
80}
81
82
83
84// vim: ts=4 sw=4