int pth_rwlock_init(pth_rwlock_t *rwlock) { if (rwlock == NULL) return pth_error(FALSE, EINVAL); rwlock->rw_state = PTH_RWLOCK_INITIALIZED; rwlock->rw_readers = 0; pth_mutex_init(&(rwlock->rw_mutex_rd)); pth_mutex_init(&(rwlock->rw_mutex_rw)); return TRUE; }
/* Allocate a mutex */ TSRM_API MUTEX_T tsrm_mutex_alloc(void) { MUTEX_T mutexp; #ifdef TSRM_WIN32 mutexp = malloc(sizeof(CRITICAL_SECTION)); InitializeCriticalSection(mutexp); #elif defined(GNUPTH) mutexp = (MUTEX_T) malloc(sizeof(*mutexp)); pth_mutex_init(mutexp); #elif defined(PTHREADS) mutexp = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); pthread_mutex_init(mutexp,NULL); #elif defined(NSAPI) mutexp = crit_init(); #elif defined(PI3WEB) mutexp = PIPlatform_allocLocalMutex(); #elif defined(TSRM_ST) mutexp = st_mutex_new(); #elif defined(BETHREADS) mutexp = (beos_ben*)malloc(sizeof(beos_ben)); mutexp->ben = 0; mutexp->sem = create_sem(1, "PHP sempahore"); #endif #ifdef THR_DEBUG printf("Mutex created thread: %d\n",mythreadid()); #endif return( mutexp ); }
/* Initialize the mutex *PRIV. If JUST_CHECK is true, only do this if it is not already initialized. */ static int mutex_pth_init (ath_mutex_t *priv, int just_check) { int err = 0; if (just_check) pth_mutex_acquire (&check_init_lock, 0, NULL); if (!*priv || !just_check) { pth_mutex_t *lock = malloc (sizeof (pth_mutex_t)); if (!lock) err = ENOMEM; if (!err) { err = pth_mutex_init (lock); if (err == FALSE) err = errno; else err = 0; if (err) free (lock); else *priv = (ath_mutex_t) lock; } } if (just_check) pth_mutex_release (&check_init_lock); return err; }
void aim_transport(instance i, xmlnode x) { ati ti; xmlnode config; char *eightbitcode, *utf8code = "UTF-8", *latin1code = "CP1252"; ti = pmalloco(i->p, sizeof(_ati)); ti->i = i; ti->xc = xdb_cache(i); log_notice(i->id, "AIM-Transport starting up for instance %s...", i->id); config = xdb_get(ti->xc, jid_new(xmlnode_pool(x), "config@-internal"), "jabber:config:aimtrans"); ti->vcard = xmlnode_new_tag_pool(i->p,"vCard"); xmlnode_put_attrib(ti->vcard,"xmlns",NS_VCARD); xmlnode_insert_node(ti->vcard,xmlnode_get_firstchild(xmlnode_get_tag(config,"vCard"))); ti->start_time = time(NULL); ti->session__list=xhash_new(101); ti->iq__callbacks = xhash_new(23); ti->pending__buddies = xhash_new(101); /* The aim.exe binary should not be necessary any more. */ ti->aimbinarydir = pstrdup(i->p, xmlnode_get_tag_data(config, "aimbinarydir")); eightbitcode = pstrdup(i->p, xmlnode_get_tag_data(config, "charset")); if( eightbitcode == NULL ) { log_notice( i->id, "Charset is not specified, using CP1252" ); eightbitcode = latin1code; } xmlnode_free(config); fromutf8 = iconv_open(eightbitcode, utf8code); if(fromutf8 == (iconv_t)(-1)) { log_error(i->id, "Conversion from %s to %s is not supported", utf8code, eightbitcode); raise(SIGINT); } toutf8 = iconv_open(utf8code, eightbitcode); if(toutf8 == (iconv_t)(-1)) { log_error(i->id, "Conversion from %s to %s is not supported", eightbitcode, utf8code); raise(SIGINT); } ti->send_buf = NULL; ti->modname = NULL; pth_mutex_init(&ti->buddies_mutex); at_init_iqcbs(ti); register_phandler(i, o_DELIVER, at_phandler, ti); pool_cleanup(i->p, at_shutdown, (void*)i); }
GroupCache::GroupCache (Layer3 * l3, Trace * t) { TRACEPRINTF (t, 4, this, "GroupCacheInit"); this->t = t; this->layer3 = l3; this->enable = 0; pos = 0; memset (updates, 0, sizeof (updates)); pth_mutex_init (&mutex); pth_cond_init (&cond); }
/* This function must be called once to initialize this module. This has to be done before a second thread is spawned. We can't do the static initialization because Pth emulation code might not be able to do a static init; in particular, it is not possible for W32. */ void initialize_module_call_pinentry (void) { static int initialized; if (!initialized) { if (pth_mutex_init (&entry_lock)) initialized = 1; } }
/* This function must be called once to initialize this module. This has to be done before a second thread is spawned. We can't do the static initialization because Pth emulation code might not be able to do a static init; in particular, it is not possible for W32. */ void initialize_module_trustlist (void) { static int initialized; if (!initialized) { if (!pth_mutex_init (&trusttable_lock)) log_fatal ("error initializing mutex: %s\n", strerror (errno)); initialized = 1; } }
/* Create a mutex */ SDL_mutex *SDL_CreateMutex(void) { SDL_mutex *mutex; /* Allocate mutex memory */ mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex)); if ( mutex ) { /* Create the mutex, with initial value signaled */ if (!pth_mutex_init(&(mutex->mutexpth_p))) { SDL_SetError("Couldn't create mutex"); SDL_free(mutex); mutex = NULL; } } else { SDL_OutOfMemory(); } return(mutex); }
DMHDBC __alloc_dbc( void ) { DMHDBC connection = NULL; mutex_entry( &mutex_lists ); connection = calloc( sizeof( *connection ), 1 ); if ( connection ) { /* * add to list of connection handles */ connection -> next_class_list = connection_root; connection_root = connection; connection -> type = HDBC_MAGIC; setup_error_head( &connection -> error, connection, SQL_HANDLE_DBC ); #ifdef HAVE_LIBPTH pth_mutex_init( &connection -> mutex ); /* * for the moment protect at the environment level */ connection -> protection_level = TS_LEVEL3; #elif HAVE_LIBPTHREAD pthread_mutex_init( &connection -> mutex, NULL ); /* * for the moment protect at the environment level */ connection -> protection_level = TS_LEVEL3; #elif HAVE_LIBTHREAD mutex_init( &connection -> mutex, USYNC_THREAD, NULL ); connection -> protection_level = TS_LEVEL3; #endif } mutex_exit( &mutex_lists ); return connection; }
/* Allocate a mutex */ TSRM_API MUTEX_T tsrm_mutex_alloc(void) {/*{{{*/ MUTEX_T mutexp; #ifdef TSRM_WIN32 mutexp = malloc(sizeof(CRITICAL_SECTION)); InitializeCriticalSection(mutexp); #elif defined(GNUPTH) mutexp = (MUTEX_T) malloc(sizeof(*mutexp)); pth_mutex_init(mutexp); #elif defined(PTHREADS) mutexp = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); pthread_mutex_init(mutexp,NULL); #elif defined(TSRM_ST) mutexp = st_mutex_new(); #endif #ifdef THR_DEBUG printf("Mutex created thread: %d\n",mythreadid()); #endif return( mutexp ); }/*}}}*/
DMHSTMT __alloc_stmt( void ) { DMHSTMT statement = NULL; mutex_entry( &mutex_lists ); statement = calloc( sizeof( *statement ), 1 ); if ( statement ) { /* * add to list of statement handles */ statement -> next_class_list = statement_root; #ifdef FAST_HANDLE_VALIDATE if ( statement_root ) { statement_root -> prev_class_list = statement; } #endif statement_root = statement; statement -> type = HSTMT_MAGIC; setup_error_head( &statement -> error, statement, SQL_HANDLE_STMT ); #ifdef HAVE_LIBPTH pth_mutex_init( &statement -> mutex ); #elif HAVE_LIBPTHREAD pthread_mutex_init( &statement -> mutex, NULL ); #elif HAVE_LIBTHREAD mutex_init( &statement -> mutex, USYNC_THREAD, NULL ); #endif } mutex_exit( &mutex_lists ); return statement; }
DMHDESC __alloc_desc( void ) { DMHDESC descriptor = NULL; mutex_entry( &mutex_lists ); descriptor = calloc( sizeof( *descriptor ), 1 ); if ( descriptor ) { /* * add to list of descriptor handles */ descriptor -> next_class_list = descriptor_root; #ifdef FAST_HANDLE_VALIDATE if ( descriptor_root ) { descriptor_root -> prev_class_list = descriptor; } #endif descriptor_root = descriptor; descriptor -> type = HDESC_MAGIC; } setup_error_head( &descriptor -> error, descriptor, SQL_HANDLE_DESC ); #ifdef HAVE_LIBPTH pth_mutex_init( &descriptor -> mutex ); #elif HAVE_LIBPTHREAD pthread_mutex_init( &descriptor -> mutex, NULL ); #elif HAVE_LIBTHREAD mutex_init( &descriptor -> mutex, USYNC_THREAD, NULL ); #endif mutex_exit( &mutex_lists ); return descriptor; }
// return 0 on success, 1 on error. uint32_t register_pipe(char* pipe_name, int pipe_depth, int pipe_width, int pipe_mode) { PipeRec* p; if(pipe_depth <= 0) { fprintf(stderr,"\nWarning: pipeHandler: pipe %s with declared depth %d set to depth=1.\n", pipe_name, pipe_depth); pipe_depth = 1; } p = find_pipe(pipe_name); // this also uses the lock. if(p != NULL) { if(p->pipe_width != pipe_width) { fprintf(stderr,"\nError: pipeHandler: redefinition of pipe %s with conflicting widths (%d or %d?)\n", pipe_name, p->pipe_width, pipe_width); return(1); } if(p->pipe_depth != pipe_depth) { fprintf(stderr,"\nError: pipeHandler: redefinition of pipe %s with conflicting depths (%d or %d?)\n", pipe_name, p->pipe_depth, pipe_depth); return(1); } if(p->pipe_mode != pipe_mode) { fprintf(stderr,"\nError: pipeHandler: redefinition of pipe %s with conflicting modes (FIFO or LIFO?)\n", pipe_name); return(1); } return(0); } PipeRec* new_p = (PipeRec*) calloc(1,sizeof(PipeRec)); new_p->pipe_name = strdup(pipe_name); new_p->pipe_width = pipe_width; new_p->pipe_depth = pipe_depth; new_p->number_of_entries = 0; new_p->write_pointer = 0; new_p->read_pointer = 0; new_p->buffer.ptr8 = (uint8_t*) calloc(1, ((pipe_depth*pipe_width)/8)*sizeof(uint8_t)); new_p->pipe_mode = pipe_mode; new_p->next = NULL; #ifdef USE_GNUPTH pth_mutex_init(&(new_p->pm)); #else pthread_mutex_init (&(new_p->pm), NULL); #endif ___LOCK___ new_p->next = pipes; pipes = new_p; ___UNLOCK___ if(log_file != NULL) { __LOCKLOG__ fprintf(log_file,"\nAdded: %s depth %d width %d pipe_mode %d.", pipe_name,pipe_depth,pipe_width, pipe_mode); fflush(log_file); __UNLOCKLOG__ }
FrSimThreadCondVar::FrSimThreadCondVar() : isLocked(false) { pth_cond_init(&cond); pth_mutex_init(&mutex); }
int main(int argc, char *argv[]) { if (argc < 5) { return 1; } auto processNum = 4; auto threadNum = atoi(argv[4]); auto dtime = atol(argv[3]) * 1000; auto isPth = std::string(argv[2]) == "pth"; auto taskNum = atoi(argv[1]); long cycles = 0; switch(taskNum) { case 1: task = task1; break; case 2: task = task2; break; case 3: task = task3; break; } time_start(); for (auto i = 0; i < processNum; ++i) { if (fork() != 0) { continue; } if (isPth) { pth_init(); pth_attr_t attr = pth_attr_new(); pth_attr_set(attr, PTH_ATTR_NAME, "task"); pth_attr_set(attr, PTH_ATTR_STACK_SIZE, 64*1024); pth_attr_set(attr, PTH_ATTR_JOINABLE, true); pth_mutex_t mutex; pth_mutex_init(&mutex); pth_cond_init(&pthCond); while (time_stop() < dtime) { for (auto i = workingNum; i < threadNum; ++i) { ++workingNum; pth_spawn(attr, task, &isPth); } int rc; if ((rc = pth_mutex_acquire(&mutex, FALSE, NULL)) != 0) { std::cout << "pthread_mutex_lock " << rc << " " << strerror(rc) << std::endl; return 3; } if (workingNum == threadNum) { if ((rc = pth_cond_await(&pthCond, &mutex, NULL)) != 0) { std::cout << "pthread_cond_wait " << rc << " " << strerror(rc) << std::endl; return 3; } } if ((rc = pth_mutex_release(&mutex)) != 0) { std::cout << "pthread_mutex_unlock " << rc << " " << strerror(rc) << std::endl; return 3; } cycles += threadNum - workingNum; } } else { pthread_attr_t attr; pthread_attr_setstacksize(&attr, 64*1024); pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutexattr_t mutexattr; pthread_mutex_init(&mutex, &mutexattr); pthread_condattr_t condattr; pthread_cond_init(&cond, &condattr); pthread_t pid; while (time_stop() < dtime) { for (auto i = workingNum; i < threadNum; ++i) { ++workingNum; if (pthread_create(&pid, NULL, task, &isPth) == -1) { return 2; } } int rc; if ((rc = pthread_mutex_lock(&mutex)) != 0) { std::cout << "pthread_mutex_lock " << rc << " " << strerror(rc) << std::endl; return 3; } if (workingNum == threadNum) { if ((rc = pthread_cond_wait(&cond, &mutex)) != 0) { std::cout << "pthread_cond_wait " << rc << " " << strerror(rc) << std::endl; return 3; } } if ((rc = pthread_mutex_unlock(&mutex)) != 0) { std::cout << "pthread_mutex_unlock " << rc << " " << strerror(rc) << std::endl; return 3; } //pthread_join(pids.front(), NULL); //pids.pop_front(); cycles += threadNum - workingNum; } } std::cout << cycles << std::endl; return 0; } for (auto i = 0; i < processNum; ++i) { wait(NULL); } return 0; }
int ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex ) { return( pth_mutex_init( mutex ) ? 0 : errno ); }
int main(int argc, char *argv[]) { int i; sigset_t ss; int sig; pth_event_t ev; /* initialize Pth library */ pth_init(); /* display test program header */ printf("This is TEST_PHILO, a Pth test showing the Five Dining Philosophers\n"); printf("\n"); printf("This is a demonstration showing the famous concurrency problem of the\n"); printf("Five Dining Philosophers as analysed 1965 by E.W.Dijkstra:\n"); printf("\n"); printf("Five philosophers are sitting around a round table, each with a bowl of\n"); printf("Chinese food in front of him. Between periods of talking they may start\n"); printf("eating whenever they want to, with their bowls being filled frequently.\n"); printf("But there are only five chopsticks available, one each to the left of\n"); printf("each bowl - and for eating Chinese food one needs two chopsticks. When\n"); printf("a philosopher wants to start eating, he must pick up the chopstick to\n"); printf("the left of his bowl and the chopstick to the right of his bowl. He\n"); printf("may find, however, that either one (or even both) of the chopsticks is\n"); printf("unavailable as it is being used by another philosopher sitting on his\n"); printf("right or left, so he has to wait.\n"); printf("\n"); printf("This situation shows classical contention under concurrency (the\n"); printf("philosophers want to grab the chopsticks) and the possibility of a\n"); printf("deadlock (all philosophers wait that the chopstick to their left becomes\n"); printf("available).\n"); printf("\n"); printf("The demonstration runs max. 60 seconds. To stop before, press CTRL-C.\n"); printf("\n"); printf("+----P1----+----P2----+----P3----+----P4----+----P5----+\n"); /* initialize the control table */ tab = (table *)malloc(sizeof(table)); if (!pth_mutex_init(&(tab->mutex))) { perror("pth_mutex_init"); exit(1); } for (i = 0; i < PHILNUM; i++) { (tab->self)[i] = i; (tab->status)[i] = thinking; if (!pth_cond_init(&((tab->condition)[i]))) { perror("pth_cond_init"); exit(1); } } /* spawn the philosopher threads */ for (i = 0; i < PHILNUM; i++) { if (((tab->tid)[i] = pth_spawn(PTH_ATTR_DEFAULT, philosopher, &((tab->self)[i]))) == NULL) { perror("pth_spawn"); exit(1); } } /* wait until 60 seconds have elapsed or CTRL-C was pressed */ sigemptyset(&ss); sigaddset(&ss, SIGINT); ev = pth_event(PTH_EVENT_TIME, pth_timeout(60,0)); pth_sigwait_ev(&ss, &sig, ev); pth_event_free(ev, PTH_FREE_ALL); /* cancel and join the philosopher threads */ for (i = 0; i < PHILNUM; i++) pth_cancel((tab->tid)[i]); while (pth_join(NULL, NULL)); /* finish display */ printf("+----------+----------+----------+----------+----------+\n"); /* free the control table */ free(tab); /* shutdown Pth library */ pth_kill(); return 0; }