/* daemon wake up */ void vntsd_daemon_wakeup(vntsd_t *vntsdp) { vcc_response_t inq_data; /* reason to wake up */ if (vntsd_vcc_ioctl(VCC_INQUIRY, 0, (void *)&inq_data) != VNTSD_SUCCESS) { vntsd_log(VNTSD_ERR_VCC_IOCTL, "vntsd_daemon_wakeup()"); return; } D1(stderr, "t@%d vntsd_daemon_wakup:msg %d port %x\n", thr_self(), inq_data.reason, inq_data.cons_no); switch (inq_data.reason) { case VCC_CONS_ADDED: do_add_cons(vntsdp, inq_data.cons_no); break; default: DERR(stderr, "t@%d daemon_wakeup:ioctl_unknown %d\n", thr_self(), inq_data.reason); vntsd_log(VNTSD_ERR_UNKNOWN_CMD, "from vcc\n"); break; } }
void __flockfile_internal(FILE *fp, int internal) { if (__isthreaded == 0) return; mutex_lock(&_LOCK(fp)); if (_LOCKOWNER(fp) == thr_self()) { _LOCKCOUNT(fp)++; if (internal) _LOCKINTERNAL(fp)++; } else { /* danger! cond_wait() is a cancellation point. */ int oldstate; thr_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate); while (_LOCKOWNER(fp) != NULL) cond_wait(&_LOCKCOND(fp), &_LOCK(fp)); thr_setcancelstate(oldstate, NULL); _LOCKOWNER(fp) = thr_self(); _LOCKCOUNT(fp) = 1; if (internal) _LOCKINTERNAL(fp) = 1; } if (_LOCKINTERNAL(fp) == 1) /* stash cancellation state and disable */ thr_setcancelstate(PTHREAD_CANCEL_DISABLE, &_LOCKCANCELSTATE(fp)); mutex_unlock(&_LOCK(fp)); }
static void zend_jit_perf_jitdump_register(const char *name, void *start, size_t size) { if (jitdump_fd >= 0) { static uint64_t id = 1; zend_perf_jitdump_load_record rec; size_t len = strlen(name); uint32_t thread_id; #if defined(__linux__) thread_id = syscall(SYS_gettid); #elif defined(__FreeBSD__) long tid; thr_self(&tid); thread_id = (uint32_t)tid; #elif defined(__OpenBSD__) thread_id = getthrid(); #elif defined(__NetBSD__) thread_id = _lwp_self(); #endif memset(&rec, 0, sizeof(rec)); rec.hdr.event = ZEND_PERF_JITDUMP_RECORD_LOAD; rec.hdr.size = sizeof(rec) + len + 1 + size; rec.hdr.time_stamp = zend_perf_timestamp(); rec.process_id = getpid(); rec.thread_id = thread_id; rec.vma = (uint64_t)(uintptr_t)start; rec.code_address = (uint64_t)(uintptr_t)start; rec.code_size = (uint64_t)size; rec.code_id = id++; zend_quiet_write(jitdump_fd, &rec, sizeof(rec)); zend_quiet_write(jitdump_fd, name, len + 1); zend_quiet_write(jitdump_fd, start, size); } }
void * sub_d(void *arg) { thread_t thr_b = (thread_t) arg; int i; thread_t thr_e, ret_thr; void *status; printf("D: In thread D...\n"); if (thr_create(NULL, 0, sub_e, NULL, THR_NEW_LWP, &thr_e)) fprintf(stderr,"Can't create thr_e\n"), exit(1); printf("D: Created thread E:%d\n", thr_e); printf("D: Continue B thread = %d\n", thr_b); thr_continue(thr_b); printf("D: Join E thread\n"); if(thr_join(thr_e,(thread_t *)&ret_thr, &status)) fprintf(stderr,"thr_join Error\n"), exit(1); printf("D: E thread (%d) returned thread (%d) w/status %d\n", thr_e, ret_thr, (int) status); /* process */ for (i=0;i<1000000*(int)thr_self();i++); printf("D: Thread exiting...\n"); thr_exit((void *)55); }
void * sub_c(void *arg) { void *status; int i; thread_t main_thr, ret_thr; main_thr = (thread_t)arg; printf("C: In thread C...\n"); if (thr_create(NULL, 0, sub_f, (void *)0, THR_BOUND|THR_DAEMON, NULL)) fprintf(stderr, "Can't create thr_f\n"), exit(1); printf("C: Join main thread\n"); if (thr_join(main_thr,(thread_t *)&ret_thr, &status)) fprintf(stderr, "thr_join Error\n"), exit(1); printf("C: Main thread (%d) returned thread (%d) w/status %d\n", main_thr, ret_thr, (int) status); /* process */ for (i=0;i<1000000*(int)thr_self();i++); printf("C: Thread exiting...\n"); thr_exit((void *)88); }
int FreeIdSet::claim_par_id() { #if FID_STATS thread_t tslf = thr_self(); tty->print("claim_par_id[%d]: sz = %d, claimed = %d\n", tslf, _sz, _claimed); #endif MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag); while (!_safepoint && _hd == end_of_list) { _waiters++; #if FID_STATS if (_waiters > 5) { tty->print("claim_par_id waiting[%d]: %d waiters, %d claimed.\n", tslf, _waiters, _claimed); } #endif _mon->wait(Mutex::_no_safepoint_check_flag); _waiters--; } if (_hd == end_of_list) { #if FID_STATS tty->print("claim_par_id[%d]: returning EOL.\n", tslf); #endif return -1; } else { int res = _hd; _hd = _ids[res]; _ids[res] = claimed; // For debugging. _claimed++; #if FID_STATS tty->print("claim_par_id[%d]: returning %d, claimed = %d.\n", tslf, res, _claimed); #endif return res; } }
static void umem_lockup(void) { umem_cache_t *cp; (void) mutex_lock(&umem_init_lock); /* * If another thread is busy initializing the library, we must * wait for it to complete (by calling umem_init()) before allowing * the fork() to proceed. */ if (umem_ready == UMEM_READY_INITING && umem_init_thr != thr_self()) { (void) mutex_unlock(&umem_init_lock); (void) umem_init(); (void) mutex_lock(&umem_init_lock); } (void) mutex_lock(&umem_cache_lock); (void) mutex_lock(&umem_update_lock); (void) mutex_lock(&umem_flags_lock); umem_lockup_cache(&umem_null_cache); for (cp = umem_null_cache.cache_prev; cp != &umem_null_cache; cp = cp->cache_prev) umem_lockup_cache(cp); umem_lockup_log_header(umem_transaction_log); umem_lockup_log_header(umem_content_log); umem_lockup_log_header(umem_failure_log); umem_lockup_log_header(umem_slab_log); (void) cond_broadcast(&umem_update_cv); vmem_sbrk_lockup(); vmem_lockup(); }
/* Set the current thread's priority. */ int __objc_thread_set_priority(int priority) { int sys_priority = 0; switch (priority) { case OBJC_THREAD_INTERACTIVE_PRIORITY: sys_priority = 300; break; default: case OBJC_THREAD_BACKGROUND_PRIORITY: sys_priority = 200; break; case OBJC_THREAD_LOW_PRIORITY: sys_priority = 1000; break; } /* Change priority */ if (thr_setprio(thr_self(), sys_priority) == 0) return 0; else return -1; }
int ldap_thr_index() { int i = 0; int free = 0; pthread_t cur = thr_self(); for (i = 1; i < MAX_THREAD_ID; ++i) { if (ldap_thr_table[i] == cur) { return (i); } /* end if */ } /* end for */ /* * not in the table, allocate a new entry */ pthread_mutex_lock(&ldap_thr_index_mutex); for (i = 1; i < MAX_THREAD_ID; ++i) { if (ldap_thr_table[i] == 0 || thr_kill(ldap_thr_table[i], 0) != 0) { ldap_thr_table[i] = cur; pthread_mutex_unlock(&ldap_thr_index_mutex); return (i); } /* end if */ } /* end for */ pthread_mutex_unlock(&ldap_thr_index_mutex); /* if table is full, return the first entry, so that it */ /* doesn't core dump */ return (0); }
unsigned long solaris_thread_id(void) { unsigned long ret; ret = (unsigned long)thr_self(); return (ret); }
/****************************************************************** * KpGetCurrentThread (Solaris Version) * * Description * * This function returns the currently executing Thread * * Author * mjb * * Created * July 5, 1994 *****************************************************************************/ KpThread_t KpGetCurrentThread (void) { return (KpThread_t) thr_self(); } /* KpGetCurrentThread */
/* create listen thread */ static boolean_t create_listen_thread(vntsd_group_t *groupp) { char err_msg[VNTSD_LINE_LEN]; int rv; assert(groupp); (void) mutex_lock(&groupp->lock); assert(groupp->num_cons); D1(stderr, "t@%d create_listen:%lld\n", thr_self(), groupp->tcp_port); if ((rv = thr_create(NULL, 0, (thr_func_t)vntsd_listen_thread, (void *)groupp, THR_DETACHED, &groupp->listen_tid)) != 0) { (void) (void) snprintf(err_msg, sizeof (err_msg), "Can not create listen thread for" "group %s tcp %llx\n", groupp->group_name, groupp->tcp_port); vntsd_log(VNTSD_ERR_CREATE_LISTEN_THR, err_msg); /* clean up group queue */ vntsd_free_que(&groupp->conspq, (clean_func_t)free_cons); groupp->listen_tid = (thread_t)-1; } (void) mutex_unlock(&groupp->lock); return (rv != 0); }
/* allocate and initialize group */ static vntsd_group_t * alloc_group(vntsd_t *vntsdp, char *group_name, uint64_t tcp_port) { vntsd_group_t *groupp; /* allocate group */ groupp = (vntsd_group_t *)malloc(sizeof (vntsd_group_t)); if (groupp == NULL) { vntsd_log(VNTSD_ERR_NO_MEM, "alloc_group"); return (NULL); } /* initialize group */ bzero(groupp, sizeof (vntsd_group_t)); (void) mutex_init(&groupp->lock, USYNC_THREAD|LOCK_ERRORCHECK, NULL); (void) cond_init(&groupp->cvp, USYNC_THREAD, NULL); if (group_name != NULL) { (void) memcpy(groupp->group_name, group_name, MAXPATHLEN); } groupp->tcp_port = tcp_port; groupp->listen_tid = (thread_t)-1; groupp->sockfd = (thread_t)-1; groupp->vntsd = vntsdp; D1(stderr, "t@%d alloc_group@%lld:%s\n", thr_self(), groupp->tcp_port, groupp->group_name); return (groupp); }
void * sub_e(void *arg) { int i; thread_t ret_thr; void *status; printf("E: In thread E...\n"); printf("E: Join A thread\n"); if(thr_join(thr_a,(thread_t *)&ret_thr, &status)) fprintf(stderr,"thr_join Error\n"), exit(1); printf("E: A thread (%d) returned thread (%d) w/status %d\n", ret_thr, ret_thr, (int) status); printf("E: Join B thread\n"); if(thr_join(thr_b,(thread_t *)&ret_thr, &status)) fprintf(stderr,"thr_join Error\n"), exit(1); printf("E: B thread (%d) returned thread (%d) w/status %d\n", thr_b, ret_thr, (int) status); printf("E: Join C thread\n"); if(thr_join(thr_c,(thread_t *)&ret_thr, &status)) fprintf(stderr,"thr_join Error\n"), exit(1); printf("E: C thread (%d) returned thread (%d) w/status %d\n", thr_c, ret_thr, (int) status); for (i=0;i<1000000*(int)thr_self();i++); printf("E: Thread exiting...\n"); thr_exit((void *)44); }
int conectarFrontEnd(in_addr_t nDireccionIP, in_port_t nPort) { SOCKET sockFrontEnd; SOCKADDR_IN their_addr; char buffer[BUF_SIZE]; char descID[DESCRIPTORID_LEN]; memset(buffer, '\0', sizeof(buffer)); sockFrontEnd = establecerConexionServidor(nDireccionIP, nPort, &their_addr); if (sockFrontEnd == INVALID_SOCKET) return -1; sprintf(buffer, "%s:%d", inet_ntoa(*(IN_ADDR *) &config.ipL), ntohs(config.puertoL)); if (ircRequest_send(sockFrontEnd, buffer, sizeof(buffer), descID, IRC_HANDSHAKE_QM) < 0) { WriteLog(log, "Query Manager", getpid(), thr_self(), "Error: recibir mensaje del Front-end al tratar de conectarse", "ERROR"); close(sockFrontEnd); return -1; } close(sockFrontEnd); return 0; }
static void sigMonitorNotify() { thread_t self = thr_self(); sysAssert(userSigMon.owner == self); sysAssert(userSigMon.count > 0); condvarSignal(&userSigMon.condvar); }
/* * When SIGUSR1 is received, exit the thread */ void catch_sigusr1(void) { rcm_log_message(RCM_DEBUG, "SIGUSR1 received in thread %d\n", thr_self()); cleanup_poll_thread(); thr_exit(NULL); }
/*! This returns the thread handle of the currently executing thread. The handle returned by this function is used for internal reasons and should not be used in any application code. On Windows, the returned value is a pseudo handle for the current thread, and it can not be used for numerical comparison. */ HANDLE QThread::currentThread() { #if defined(_OS_SOLARIS_) return (HANDLE) thr_self(); #else return (HANDLE) pthread_self(); #endif }
/* * acquire_writer() the client is going to be writer. * insert the client to the head of the console client queue. */ static int acquire_writer(vntsd_client_t *clientp) { vntsd_cons_t *consp; vntsd_client_t *writerp; int rv; D1(stderr, "t@%d:acuire_writer :client@%d\n", thr_self(), clientp->sockfd); assert(clientp != NULL); consp = clientp->cons; assert(consp); (void) mutex_lock(&consp->lock); assert(consp->clientpq != NULL); if (consp->clientpq->handle == clientp) { /* clientp is a writer already */ (void) mutex_unlock(&consp->lock); return (VNTSD_SUCCESS); } /* current writer */ writerp = (vntsd_client_t *)(consp->clientpq->handle); (void) mutex_lock(&writerp->lock); rv = vntsd_que_rm(&(consp->clientpq), clientp); assert(rv == VNTSD_SUCCESS); (void) mutex_lock(&clientp->lock); /* move client to be first in the console queue */ consp->clientpq->handle = clientp; /* move previous writer to be the second in the queue */ rv = vntsd_que_insert_after(consp->clientpq, clientp, writerp); (void) mutex_unlock(&consp->lock); (void) mutex_unlock(&writerp->lock); (void) mutex_unlock(&clientp->lock); if (rv != VNTSD_SUCCESS) { return (rv); } /* write warning message to the writer */ if ((rv = vntsd_write_line(writerp, gettext("Warning: Console connection forced into read-only mode"))) != VNTSD_SUCCESS) { return (rv); } return (VNTSD_SUCCESS); }
JNIEXPORT jlong JNICALL Java_sun_nio_ch_NativeThread_current(JNIEnv *env, jclass cl) { #ifdef __solaris__ return (jlong)thr_self(); #else return (jlong)pthread_self(); #endif }
int _nscd_is_getent_ctx_in_use( nscd_getent_context_t *ctx) { int in_use; char *me = "_nscd_getent_ctx_in_use"; (void) mutex_lock(&ctx->getent_mutex); _NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG) (me, "in_use = %d, ctx->thr_id = %d, thread id = %d\n", ctx->in_use, ctx->thr_id, thr_self()); in_use = ctx->in_use; if (in_use == 1 && ctx->thr_id == thr_self()) in_use = 0; (void) mutex_unlock(&ctx->getent_mutex); return (in_use); }
void ExitHandler(int sig) { thread_t id; id = thr_self(); fprintf(stderr, "ExitHandler thread id: %d\n", id); thr_exit(0); }
void CThread::SetThreadInfo() { #ifdef TARGET_FREEBSD #if __FreeBSD_version < 900031 long lwpid; thr_self(&lwpid); m_ThreadOpaque.LwpId = lwpid; #else m_ThreadOpaque.LwpId = pthread_getthreadid_np(); #endif #elif defined(TARGET_ANDROID) m_ThreadOpaque.LwpId = gettid(); #else m_ThreadOpaque.LwpId = syscall(SYS_gettid); #endif #if defined(HAVE_PTHREAD_SETNAME_NP) #ifdef TARGET_DARWIN #if(__MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 || __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200) pthread_setname_np(m_ThreadName.c_str()); #endif #else pthread_setname_np(m_ThreadId, m_ThreadName.c_str()); #endif #elif defined(HAVE_PTHREAD_SET_NAME_NP) pthread_set_name_np(m_ThreadId, m_ThreadName.c_str()); #endif #ifdef RLIMIT_NICE // get user max prio struct rlimit limit; int userMaxPrio; if (getrlimit(RLIMIT_NICE, &limit) == 0) { userMaxPrio = limit.rlim_cur - 20; if (userMaxPrio < 0) userMaxPrio = 0; } else userMaxPrio = 0; if (geteuid() == 0) userMaxPrio = GetMaxPriority(); // if the user does not have an entry in limits.conf the following // call will fail if (userMaxPrio > 0) { // start thread with nice level of appication int appNice = getpriority(PRIO_PROCESS, getpid()); if (setpriority(PRIO_PROCESS, m_ThreadOpaque.LwpId, appNice) != 0) if (logger) logger->Log(LOGERROR, "%s: error %s", __FUNCTION__, strerror(errno)); } #endif }
static void *first_thread() { int i = 0; fprintf(stderr, "first_thread id: %d\n", thr_self()); while (first_active){ fprintf(stderr, "first_thread: %d\n", i++); sleep(2); } fprintf(stderr, "first_thread exit\n"); }
static void sigMonitorExit() { thread_t self = thr_self(); sysAssert(userSigMon.owner == self); sysAssert(userSigMon.count > 0); if (--userSigMon.count == 0) { userSigMon.owner = 0; mutex_unlock(&userSigMon.mutex); } }
static void sigMonitorEnter() { thread_t self = thr_self(); if (userSigMon.owner == self) { userSigMon.count++; } else { mutex_lock(&userSigMon.mutex); userSigMon.owner = self; userSigMon.count = 1; } }
static void *second_thread() { int i = 0; fprintf(stderr, "second_thread id: %d\n", thr_self()); while (second_active){ fprintf(stderr, "second_thread: %d\n", i++); sleep(3); } fprintf(stderr, "second_thread exit\n"); }
void glthread_recursive_lock_lock (gl_recursive_lock_t *lock) { thread_t self = thr_self (); if (lock->owner != self) { if (mutex_lock (&lock->mutex) != 0) abort (); lock->owner = self; } if (++(lock->depth) == 0) /* wraparound? */ abort (); }
void * sub_b(void *arg) { int i; printf("B: In thread B...\n"); /* process */ for (i=0;i<1000000*(int)thr_self();i++); printf("B: Thread exiting...\n"); thr_exit((void *)66); }
main() { int i; struct sigaction act; act.sa_handler = ExitHandler; (void) sigemptyset(&act.sa_mask); (void) sigaction(SIGTERM, &act, NULL); mutex_init(&first_mutex, 0 , 0); mutex_init(&second_mutex, 0 , 0); main_thread = thr_self(); thr_create(NULL,0,first_thread,0,THR_NEW_LWP,&one_tid); thr_create(NULL,0,second_thread,0,THR_NEW_LWP,&two_tid); for (i = 0; i < 10; i++){ fprintf(stderr, "main loop: %d\n", i); if (i == 5) { thr_kill(one_tid, SIGTERM); } sleep(3); } thr_kill(two_tid, SIGTERM); sleep(5); fprintf(stderr, "main exit\n"); }