int main(int argc, char* argv[]) { pid_t child_id; //shared memory key_t memKey = 6655; /* A key to access shared memory segments */ int memSize = sizeof(SharedMem); int memFlag = 1023; /* Call permissions and modes are set. */ sharingMemory(memKey, memSize, memFlag); /*shared mutex*/ pthread_mutexattr_t attr1, attr2; pthread_mutexattr_setpshared (&attr1, PTHREAD_PROCESS_SHARED); pthread_mutexattr_setpshared (&attr2, PTHREAD_PROCESS_SHARED); pthread_mutex_init (&shmem_ptr->lock, &attr1); pthread_mutex_init (&shmem_ptr->lock2, &attr2); /*shared conditions*/ pthread_condattr_t spaceAttr, itemAttr; pthread_condattr_setpshared(&spaceAttr, PTHREAD_PROCESS_SHARED); pthread_condattr_setpshared(&itemAttr, PTHREAD_PROCESS_SHARED); pthread_cond_init(&shmem_ptr->SpaceAvailable, &spaceAttr); pthread_cond_init(&shmem_ptr->ItemAvailable, &itemAttr); //split code if ((child_id = fork()) == -1) { printf("Could not fork()\n"); exit(1); } if (child_id != 0) { //consumer pid_t child2_id; if ((child2_id = fork()) == -1) { printf("Could not fork()\n"); exit(1); } if(child2_id != 0){ //consumer char keystr[10]; sprintf(keystr,"%d", memKey); execl("./consum", "consum", keystr, NULL); /* done with the program, so detach the shared segment and terminate */ shmdt ( (void *) shmem_ptr); printf("Finished execution \n"); }else{ //pblack char keystr[10]; sprintf(keystr,"%d", memKey); execl("./pBlack", "pBlack", keystr, NULL); } }else{ //pgreen char keystr[10]; sprintf(keystr,"%d", memKey); execl("./pGreen", "pGreen", keystr, NULL); } return 0; }
shm_data* create_shm_channel(char* fldes) { shm_unlink(fldes); // fprintf(stdout, "fldes is: %s.\n", fldes); int shm_fldes = shm_open(fldes, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); if (shm_fldes < 0) { fprintf(stderr, "Error %d (%s) on server proxy shm_open.\n", errno, strerror(errno)); fflush(stdout); // exit(1); } ftruncate(shm_fldes, segment_size); shm_data* mem = mmap(NULL, segment_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fldes, 0); close(shm_fldes); if(mem->file_length != 0 || mem->bytes_written != 0) { fprintf(stderr, "shared memory segment should have been set zero.\n"); fflush(stdout); // exit(1); } mem->file_length = 0; mem->bytes_written = 0; // initialize shared memory mutex and condition variables pthread_mutexattr_t memory_attr; pthread_mutexattr_init(&memory_attr); pthread_mutexattr_setpshared(&memory_attr, PTHREAD_PROCESS_SHARED); pthread_mutex_init(&mem->data_mutex, &memory_attr); pthread_mutexattr_t filelen_attr; pthread_mutexattr_init(&filelen_attr); pthread_mutexattr_setpshared(&filelen_attr, PTHREAD_PROCESS_SHARED); pthread_mutex_init(&mem->file_len_mutex, &filelen_attr); pthread_condattr_t cache_attr; pthread_condattr_init(&cache_attr); pthread_condattr_setpshared(&cache_attr, PTHREAD_PROCESS_SHARED); pthread_cond_init(&mem->cache_cond, &cache_attr); pthread_condattr_t proxy_attr; pthread_condattr_init(&proxy_attr); pthread_condattr_setpshared(&proxy_attr, PTHREAD_PROCESS_SHARED); pthread_cond_init(&mem->proxy_cond, &proxy_attr); return mem; }
int barrier_init(barrier_t *barrier,int needed) { barrier->needed = needed; barrier->called = 0; if(with_fork){ pthread_condattr_t cattr; pthread_mutexattr_t mattr; pthread_mutexattr_init(&mattr); if(pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED)){ perror("Mutex PTHREAD_PROCESS_SHARED"); exit(1); } pthread_condattr_init(&cattr); if(pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED)){ perror("Cond PTHREAD_PROCESS_SHARED"); exit(1); } pthread_mutex_init(&barrier->mutex,&mattr); pthread_cond_init(&barrier->cond, &cattr); }else{ pthread_mutex_init(&barrier->mutex,NULL); pthread_cond_init(&barrier->cond,NULL); } return 0; }
static ledger_status create_locks(ledger_partition *partition, int fd) { ledger_status rc; ledger_partition_locks locks; pthread_mutexattr_t mattr; pthread_condattr_t cattr; rc = pthread_mutexattr_init(&mattr); ledger_check_rc(rc == 0, LEDGER_ERR_GENERAL, "Failed to initialize mutex attribute"); rc = pthread_condattr_init(&cattr); ledger_check_rc(rc == 0, LEDGER_ERR_GENERAL, "Failed to initialize cond attribute"); rc = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); ledger_check_rc(rc == 0, LEDGER_ERR_GENERAL, "Failed to set mutex attribute to shared"); rc = pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED); ledger_check_rc(rc == 0, LEDGER_ERR_GENERAL, "Failed to set cond attribute to shared"); rc = pthread_mutex_init(&locks.rotate_lock, &mattr); ledger_check_rc(rc == 0, LEDGER_ERR_GENERAL, "Failed to initialize journal rotate mutex"); rc = pthread_cond_init(&locks.rotate_cond, &cattr); ledger_check_rc(rc == 0, LEDGER_ERR_GENERAL, "Failed to initialize journal rotate cond"); rc = ledger_pwrite(fd, (void *)&locks, sizeof(ledger_partition_locks), 0); ledger_check_rc(rc, LEDGER_ERR_IO, "Failed to write meta number of entries"); return LEDGER_OK; error: return rc; }
TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) { #if defined(__BIONIC__) // This tests a bionic implementation detail. pthread_condattr_t attr; pthread_condattr_init(&attr); ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); pthread_cond_t cond_var; ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr)); ASSERT_EQ(0, pthread_cond_signal(&cond_var)); ASSERT_EQ(0, pthread_cond_broadcast(&cond_var)); attr = static_cast<pthread_condattr_t>(cond_var.value); clockid_t clock; ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); ASSERT_EQ(CLOCK_MONOTONIC, clock); int pshared; ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared); #else // __BIONIC__ GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif // __BIONIC__ }
int uv_cond_init(uv_cond_t* cond) { pthread_condattr_t attr; if (pthread_condattr_init(&attr)) return -1; #ifndef __ANDROID__ if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)) #else if (pthread_condattr_setpshared(&attr, CLOCK_MONOTONIC)) #endif goto error2; if (pthread_cond_init(cond, &attr)) goto error2; if (pthread_condattr_destroy(&attr)) goto error; return 0; error: pthread_cond_destroy(cond); error2: pthread_condattr_destroy(&attr); return -1; }
void init_ring_buffer(struct ring_buffer* rbuff) { rbuff->request_writes = 0; rbuff->request_reads = 0; rbuff->response_writes = 0; rbuff->response_reads = 0; pthread_mutexattr_t mattr; pthread_mutexattr_init(&mattr); pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); pthread_mutex_init(&rbuff->data_mutex, &mattr); pthread_condattr_t cattr; pthread_condattr_init(&cattr); pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED); pthread_cond_init(&rbuff->nonempty, &cattr); int i; for(i = 0; i < MAXSIZE; i++) { pthread_cond_init(&rbuff->response_ready[i], &cattr); rbuff->buffer[i].request_client.response = i; } }
epicsMutexOSD * epicsMutexOsdCreate(void) { epicsMutexOSD *pmutex; int status; pmutex = callocMustSucceed(1, sizeof(*pmutex), "epicsMutexOsdCreate"); status = pthread_mutexattr_init(&pmutex->mutexAttr); checkStatusQuit(status, "pthread_mutexattr_init", "epicsMutexOsdCreate"); #if defined _POSIX_THREAD_PRIO_INHERIT status = pthread_mutexattr_setprotocol( &pmutex->mutexAttr,PTHREAD_PRIO_INHERIT); if (errVerbose) checkStatus(status, "pthread_mutexattr_setprotocal"); #endif /*_POSIX_THREAD_PRIO_INHERIT*/ status = pthread_mutex_init(&pmutex->lock, &pmutex->mutexAttr); checkStatusQuit(status, "pthread_mutex_init", "epicsMutexOsdCreate"); #if defined _POSIX_THREAD_PROCESS_SHARED status = pthread_condattr_init(&pmutex->condAttr); checkStatus(status, "pthread_condattr_init"); status = pthread_condattr_setpshared(&pmutex->condAttr, PTHREAD_PROCESS_PRIVATE); checkStatus(status, "pthread_condattr_setpshared"); status = pthread_cond_init(&pmutex->waitToBeOwner, &pmutex->condAttr); #else status = pthread_cond_init(&pmutex->waitToBeOwner, 0); #endif /*_POSIX_THREAD_PROCESS_SHARED*/ checkStatusQuit(status, "pthread_cond_init", "epicsMutexOsdCreate"); return pmutex; }
/* init shm header */ void shm_init_header( ssm_header *header, int data_size, int history_num, ssmTimeT cycle ) { pthread_mutexattr_t mattr; pthread_condattr_t cattr; header->tid_top = SSM_TID_SP - 1; /* 初期位置 */ header->size = data_size; /* データサイズ */ header->num = history_num; /* 履歴数 */ //header->table_size = hsize; /* テーブルサイズ */ header->cycle = cycle; /* データ最小サイクル */ header->data_off = sizeof( ssm_header ); /* データの先頭アドレス */ header->times_off = header->data_off + ( data_size * history_num ); /* 時刻の先頭アドレス */ //header->table_off = header->times_off + sizeof( ssmTimeT ) * hsize ; /* time tableの先頭アドレス */ /* 同期用mutexの初期化 */ pthread_mutexattr_init(&mattr); pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); pthread_mutex_init(&header->mutex, &mattr); pthread_condattr_init(&cattr); pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED); pthread_cond_init(&header->cond, &cattr); pthread_mutexattr_destroy( &mattr ); pthread_condattr_destroy( &cattr ); }
int main(){ pthread_mutexattr_t mutex_attr; pthread_condattr_t cond_attr; buffer.occupied = buffer.nextin = buffer.nextout = 0; pthread_mutexattr_init(&mutex_attr); pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED); pthread_mutex_init(&buffer.lock, &mutex_attr); pthread_condattr_init(&cond_attr); pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED); pthread_cond_init(&buffer.less, &cond_attr); pthread_cond_init(&buffer.more, &cond_attr); pthread_t cons, prod; int idCons = 0, idProd = 1; pthread_create(&cons, NULL, (void *) conscons, (void *) &idCons); pthread_create(&prod, NULL, (void *) prodprod, (void *) &idProd); pthread_join(cons, NULL); pthread_join(prod, NULL); return 0; }
CBinarySemaphore::CBinarySemaphore(bool isFull, bool isProcessShared) : mCounter(1) { Int32 retVal; pthread_mutexattr_t mutexAttr; retVal = pthread_mutexattr_init(&mutexAttr); sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to init Mutex-Attribute!"); retVal = pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_ERRORCHECK); sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to set Mutex-Type!"); retVal = pthread_mutex_init(&mMutex, &mutexAttr); sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to init Mutex!"); retVal = pthread_mutexattr_destroy(&mutexAttr); sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to destroy Mutex-Attribute!"); pthread_condattr_t conditionAttr; retVal = pthread_condattr_init(&conditionAttr); sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to init Condition-Attribute!"); retVal = pthread_condattr_setpshared(&conditionAttr, isProcessShared ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE); sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to set Condition-Attribute!"); pthread_cond_init(&mCondition, &conditionAttr); pthread_condattr_destroy(&conditionAttr); if(false == isFull) { mCounter = 0; } }
int fifo_init(struct fifo *fifo, size_t size, bool_t prio_inherit) { #if defined(HAVE_LIBPTHREAD) && !defined(SDL_FIFOS) #ifndef _POSIX_THREAD_PROCESS_SHARED #error "no _POSIX_THREAD_PROCESS_SHARED" #endif /* _POSIX_THREAD_PROCESS_SHARED */ /* linux multi-process locking */ pthread_mutexattr_t mutex_attr; pthread_condattr_t cond_attr; struct utsname utsname; int err; if ((err = pthread_mutexattr_init(&mutex_attr)) < 0) { return err; } if (prio_inherit) { if ((err = pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED)) < 0) { return err; } #ifdef _POSIX_THREAD_PRIO_INHERIT /* only on PREEMPT kernels */ if ((err = uname(&utsname)) < 0) { return err; } if (!RUNNING_ON_VALGRIND && strstr(utsname.version, "PREEMPT") != NULL) { if ((err = pthread_mutexattr_setprotocol(&mutex_attr, PTHREAD_PRIO_INHERIT)) < 0) { return err; } } #endif /* _POSIX_THREAD_PRIO_INHERIT */ } if ((err = pthread_mutex_init(&fifo->mutex, &mutex_attr)) < 0) { return err; } if ((err = pthread_condattr_init(&cond_attr)) < 0) { return err; } if (prio_inherit) { if ((err = pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED)) < 0) { return err; } } if ((err = pthread_cond_init(&fifo->cond, &cond_attr)) < 0) { return err; } #else /* cross platform locks */ fifo->mutex = SDL_CreateMutex(); fifo->cond = SDL_CreateCond(); #endif fifo->rptr = 0; fifo->wptr = 0; fifo->size = size; return 0; }
/** \brief Initialize the condition variable taking the condition * attributes into account * * \b os_condInit calls \b pthread_cond_init to intialize the posix condition * variable. * * In case the scope attribute is \b OS_SCOPE_SHARED, the posix * condition variable "pshared" attribute is set to \b PTHREAD_PROCESS_SHARED * otherwise it is set to \b PTHREAD_PROCESS_PRIVATE. */ os_result os_condInit ( os_cond *cond, os_mutex *dummymtx, const os_condAttr *condAttr) { pthread_condattr_t mattr; int result = 0; os_result rv; assert (cond != NULL); assert (condAttr != NULL); #ifdef OSPL_STRICT_MEM assert(cond->signature != OS_COND_MAGIC_SIG); #endif pthread_condattr_init (&mattr); if (condAttr->scopeAttr == OS_SCOPE_SHARED) { result = pthread_condattr_setpshared (&mattr, PTHREAD_PROCESS_SHARED); } else { result = pthread_condattr_setpshared (&mattr, PTHREAD_PROCESS_PRIVATE); } if (result == 0) { #ifdef OSPL_STRICT_MEM result = pthread_cond_init (&cond->cond, &mattr); #else result = pthread_cond_init (cond, &mattr); if (result == EBUSY) { os_condDestroy (cond); result = pthread_cond_init (cond, &mattr); } #endif } pthread_condattr_destroy (&mattr); if (result == 0) { #ifdef OSPL_STRICT_MEM cond->signature = OS_COND_MAGIC_SIG; #endif rv = os_resultSuccess; } else { rv = os_resultFail; } return rv; }
void Pthread_condattr_setpshared(pthread_condattr_t * attr, int flag) { int n; if ((n = pthread_condattr_setpshared(attr, flag)) == 0) return; errno = n; fprintf(stderr, "pthread_condattr_setpshared error"); }
int pthread_cond_init_shared ( pthread_cond_t **cond_p ) { static pthread_cond_t cond_initial = PTHREAD_COND_INITIALIZER; *cond_p = shm_malloc ( sizeof ( **cond_p ) ); memcpy ( *cond_p, &cond_initial, sizeof ( cond_initial ) ); pthread_condattr_t attr; pthread_condattr_init ( &attr ); pthread_condattr_setpshared ( &attr, PTHREAD_PROCESS_SHARED ); return pthread_cond_init ( *cond_p, &attr ); }
void init_sync(struct Sync* sync) { // These structures are used to initialize mutexes // and condition variables. We will use them to set // the PTHREAD_PROCESS_SHARED attribute, which enables // more than one process (and any thread in any of those // processes) to access the mutex and condition variable. pthread_mutexattr_t mutex_attributes; pthread_condattr_t condition_attributes; // These methods initialize the attribute structures // with default values so that we must only change // the one we are interested in. if (pthread_mutexattr_init(&mutex_attributes) != 0) { throw("Error initializing mutex attributes"); } if (pthread_condattr_init(&condition_attributes) != 0) { throw("Error initializing condition variable attributes"); } // Here we set the "process-shared"-attribute of the mutex // and condition variable to PTHREAD_PROCESS_SHARED. This // means multiple processes may access these objects. If // we wouldn't do this, the attribute would be PTHREAD_PROCESS // _PRIVATE, where only threads created by the process who // initialized the objects would be allowed to access them. // By passing PTHREAD_PROCESS_SHARED the objects may also live // longer than the process creating them. // clang-format off if (pthread_mutexattr_setpshared( &mutex_attributes, PTHREAD_PROCESS_SHARED) != 0) { throw("Error setting process-shared attribute for mutex"); } if (pthread_condattr_setpshared( &condition_attributes, PTHREAD_PROCESS_SHARED) != 0) { throw("Error setting process-shared attribute for condition variable"); } // clang-format on // Initialize the mutex and condition variable and pass the attributes if (pthread_mutex_init(&sync->mutex, &mutex_attributes) != 0) { throw("Error initializing mutex"); } if (pthread_cond_init(&sync->condition, &condition_attributes) != 0) { throw("Error initializing condition variable"); } // Destroy the attribute objects if (pthread_mutexattr_destroy(&mutex_attributes)) { throw("Error destroying mutex attributes"); } if (pthread_condattr_destroy(&condition_attributes)) { throw("Error destroying condition variable attributes"); } }
qp_cond_t qp_cond_create(qp_cond_t cond, bool shared) { pthread_condattr_t attr; if (NULL == cond) { cond = (qp_cond_t)qp_alloc(sizeof(struct qp_ipc_cond_s)); if (NULL == cond) { return NULL; } memset(cond, 0, sizeof(struct qp_ipc_cond_s)); qp_cond_set_alloced(cond); } else { memset(cond, 0, sizeof(struct qp_ipc_cond_s)); } if (QP_SUCCESS != pthread_condattr_init(&attr)) { qp_cond_is_alloced(cond) ? qp_free(cond) : 1; return NULL; } if (NULL == qp_lock_init(&cond->cond_lock, shared, false)) { qp_cond_is_alloced(cond) ? qp_free(cond) : 1; return NULL; } #ifdef _POSIX_THREAD_PROCESS_SHARED if (shared) { if (QP_SUCCESS != pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) { pthread_condattr_destroy(&attr); qp_lock_destroy(&cond->cond_lock); qp_cond_is_alloced(cond) ? qp_free(cond) : 1; return NULL; } qp_cond_set_shared(cond); } #endif if (QP_SUCCESS != pthread_cond_init(&(cond->cond), &attr)) { pthread_condattr_destroy(&attr); qp_lock_destroy(&cond->cond_lock); qp_cond_is_alloced(cond) ? qp_free(cond) : 1; return NULL; } qp_cond_set_inited(cond); return cond; }
int main() { /* Make sure there is process-shared capability. */ #ifndef PTHREAD_PROCESS_SHARED fprintf(stderr, "process-shared attribute is not available for testing\n"); return PTS_UNRESOLVED; #endif pthread_condattr_t attr[NUM_OF_CONDATTR]; int ret, i, pshared; for (i = 0; i < NUM_OF_CONDATTR; i++) { /* Initialize a cond attributes object */ if (pthread_condattr_init(&attr[i]) != 0) { perror("Error at pthread_condattr_init()\n"); return PTS_UNRESOLVED; } /* Set 'pshared' to PTHREAD_PROCESS_PRIVATE. */ ret = pthread_condattr_setpshared(&attr[i], PTHREAD_PROCESS_PRIVATE); if (ret != 0) { printf ("Test FAILED: Could not set pshared to PTHREAD_PROCESS_PRIVATE, error: %d\n", ret); return PTS_FAIL; } /* Get 'pshared'. It should be PTHREAD_PROCESS_PRIVATE. */ if (pthread_condattr_getpshared(&attr[i], &pshared) != 0) { printf ("Test FAILED: obtaining the wrong process-shared attribute, expected PTHREAD_PROCESS_PRIVATE, but got: %d\n", pshared); return PTS_FAIL; } if (pshared != PTHREAD_PROCESS_PRIVATE) { printf("Test FAILED: Incorrect pshared value: %d\n", pshared); return PTS_FAIL; } /* Destory the cond attributes object */ if (pthread_condattr_destroy(&attr[i]) != 0) { perror("Error at pthread_condattr_destroy()\n"); return PTS_UNRESOLVED; } } printf("Test PASSED\n"); return PTS_PASS; }
void pc_start(int port, int us) { assert(!init_check); init_check = 1; init(&request_list); s_port = port; use_shared = us; args = (struct pt_args_t **) malloc(sizeof(struct pt_args_t*)*MAX_PROXY_THREADS); pthreads = (pthread_t**) malloc(sizeof(pthread_t)*MAX_PROXY_THREADS); int i; for(i = 0; i < MAX_PROXY_THREADS; ++i) { args[i] = (struct pt_args_t*) malloc(sizeof(struct pt_args_t)); args[i]->request_list = &request_list; args[i]->done = 0; args[i]->use_shared = us; args[i]->id = i; if(use_shared) { if( shared_manage( &(args[i]->share), &(args[i]->shmid), i+0xab, sizeof(struct shm_thread_t)) ) exit(1); args[i]->share->proxy = 1; if(!args[i]->share->init) { { pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); if(pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) perror("shm_mutex"); if(pthread_mutex_init(&(args[i]->share->lock), &attr)) perror("shm_mutex_init"); pthread_mutexattr_destroy(&attr); } { pthread_condattr_t attr; pthread_condattr_init(&attr); if(pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) perror("shm_cond"); if(pthread_cond_init(&(args[i]->share->sig), &attr)) perror("shm_cond_init"); pthread_condattr_destroy(&attr); } args[i]->share->init = 1; } } else args[i]->share = NULL; pthreads[i] = (pthread_t *) malloc(sizeof(pthread_t)); assert(pthreads[i]); pthread_create(pthreads[i], NULL, pt_thread, (void*) args[i]); } pthread_create(&manager, NULL, pc_manager, NULL); }
void test( void ) { pthread_condattr_t attribute; int pshared; int result; pshared = PTHREAD_PROCESS_SHARED; pshared = PTHREAD_PROCESS_PRIVATE; result = pthread_condattr_setpshared( &attribute, pshared ); }
// Constructor CondVariableXp::CondVariableXp (clockid_t clk_id, int pshared ){ pthread_condattr_t attr; ERROR_CHECK_RET (pthread_condattr_init (&attr), "CondVariableXp", "pthread_condattr_init"); ERROR_CHECK_RET (pthread_condattr_setclock (&attr, clk_id), "CondVariableXp", "pthread_condattr_setclock"); ERROR_CHECK_RET (pthread_condattr_setpshared (&attr, pshared), "CondVariableXp", "pthread_condattr_setpshared"); ERROR_CHECK_RET (pthread_cond_init (&condVar, &attr), "CondVariableXp", "pthread_cond_init"); ERROR_CHECK_RET (pthread_condattr_destroy (&attr), "CondVariableXp", "pthread_condattr_destroy"); }
int unpipe_create(int size_log2) { int id; long pagesize; struct unpipe_ctx *p; pthread_mutexattr_t m_attr; pthread_condattr_t c_attr_r; pthread_condattr_t c_attr_w; pagesize = sysconf(_SC_PAGESIZE); assert(sizeof(struct unpipe_ctx *) < pagesize); id = shmget(IPC_PRIVATE, pagesize + (1<<size_log2), 0644 | IPC_CREAT); if (id == -1) return id; p = shmat(id, NULL, 0); // FIXME: error check memset(p, '\0', sizeof(struct unpipe_ctx)); p->id = id; p->size = 1<<size_log2; p->base_offset = pagesize; pthread_mutexattr_init(&m_attr); pthread_condattr_init(&c_attr_r); pthread_condattr_init(&c_attr_w); pthread_mutexattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED); pthread_condattr_setpshared(&c_attr_r, PTHREAD_PROCESS_SHARED); pthread_condattr_setpshared(&c_attr_w, PTHREAD_PROCESS_SHARED); pthread_mutex_init(&p->lock, &m_attr); pthread_cond_init(&p->r_cv, &c_attr_r); pthread_cond_init(&p->w_cv, &c_attr_w); shmdt(p); // FIXME: error check return id; }
/** ---------------------------------------------------------------------------------- */ ConditionAttributes::ConditionAttributes(bool shared) { int result = pthread_condattr_init(&m_attr); if( 0 != result ) // THROW(ml::common::exception::RuntimeException, "Error encountered during initialization", result); throw("Error encountered during initialization"); if( 0 != (result = pthread_condattr_setpshared(&m_attr, shared ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE)) ) { pthread_condattr_destroy(&m_attr); // THROW(ml::common::exception::RuntimeException, "Error encountered while setting shared status", result); throw("Error encountered while setting shared status"); } }
int xsim_cond_init(xsim_cond_t *cond) { pthread_mutexattr_t mattr; pthread_condattr_t cattr; pthread_mutexattr_init(&mattr); pthread_mutexattr_setpshared(&mattr, 1); pthread_mutex_init(&cond->lock, &mattr); pthread_condattr_init(&cattr); pthread_condattr_setpshared(&cattr, 1); pthread_cond_init(&cond->cond, &cattr); return XSIM_SUCCESS; }
s32 tbsb_init(shm_t *_pbsb) { if (NULL == _pbsb) { return -1; } //init buffer _pbsb->_buf_next_write_offset = 0; _pbsb->_buf_next_read_offset = 0; //init headers info //_pbsb->_header_size = sizeof(t_header_info); //init packs info _pbsb->_max_packet_num = _pbsb->_buf_packet_num; _pbsb->_cur_packet_num = 0; //init shm_info _pbsb->_shm_index_first = 0; _pbsb->_shm_index_last = _pbsb->_buf_packet_num - 1; _pbsb->_shm_index_for_next_read = 0; _pbsb->_shm_index_for_next_write = 0; _pbsb->_shm_index_for_being_used = -1;//no packet being used //init core if (_pbsb->_is_cross_process) { pthread_mutexattr_init(&_pbsb->_mutex_attr); pthread_mutexattr_setpshared(&_pbsb->_mutex_attr, PTHREAD_PROCESS_SHARED); pthread_condattr_init(&_pbsb->_cond_attr); pthread_condattr_setpshared(&_pbsb->_cond_attr, PTHREAD_PROCESS_SHARED); } pthread_mutex_init(&_pbsb->_mutex, &_pbsb->_mutex_attr); pthread_cond_init(&_pbsb->_cond, &_pbsb->_cond_attr); //reset buffers u8 *_buf_start_addr = (u8 *)_pbsb + _pbsb->_buf_start_offset; u8 *_header_addr = (u8 *)_pbsb + _pbsb->_header_offset; t_packet_info *_packet_addr = (t_packet_info *)((u8 *)_pbsb + _pbsb->_packet_offset); memset(_buf_start_addr, 0, _pbsb->_buf_total_size); memset(_header_addr, 0, sizeof(t_header_info) * _pbsb->_buf_packet_num); memset(_packet_addr, 0, sizeof(t_packet_info) * _pbsb->_buf_packet_num); return 0; }
semaphore *init_mutex() { semaphore *semap; conditional *cond; pthread_mutexattr_t attrmutex; pthread_condattr_t attrcond; int fd = open(SEMAPHORE_1, O_RDWR | O_CREAT | O_EXCL, 0666); if (fd < 0) { printf("Cannot open/create semaphore file\n"); printf("%s\n", strerror(errno)); int remove_res = remove(SEMAPHORE_1); if (remove_res != 0) { printf("Cannot delete %s\n file", SEMAPHORE_1); } //return NULL; } int fd_cond = open(SEMAPHORE_2, O_RDWR | O_CREAT | O_EXCL, 0666); if (fd < 0) { printf("Cannot open/create conditional file\n"); printf("%s\n", strerror(errno)); int remove_res = remove(SEMAPHORE_2); if (remove_res != 0) { printf("Cannot delete %s\n file", SEMAPHORE_2); } exit(0); //return NULL; } (void) ftruncate(fd, sizeof(semaphore)); (void) ftruncate(fd_cond, sizeof(conditional)); (void) pthread_mutexattr_init(&attrmutex); (void) pthread_mutexattr_setpshared(&attrmutex, PTHREAD_PROCESS_SHARED); (void) pthread_condattr_init(&attrcond); (void) pthread_condattr_setpshared(&attrcond, PTHREAD_PROCESS_SHARED); semap = (semaphore *)mmap(NULL, sizeof(semaphore), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); cond = (conditional *)mmap(NULL, sizeof(conditional), PROT_READ | PROT_WRITE, MAP_SHARED, fd_cond, 0); close(fd); close(fd_cond); (void) pthread_mutex_init(&semap->pmutex, &attrmutex); (void) pthread_mutex_init(&cond->mutex, &attrmutex); (void) pthread_cond_init(&semap->pcond, &attrcond); (void) pthread_cond_init(&cond->cond, &attrcond); semap->flag = 0; cond->flag = 0; return semap; }
int rt_cond_create(RT_COND *cond, const char *name) { pthread_mutexattr_t mattr; struct alchemy_cond *ccb; pthread_condattr_t cattr; struct service svc; if (threadobj_async_p()) return -EPERM; COPPERPLATE_PROTECT(svc); ccb = xnmalloc(sizeof(*ccb)); if (ccb == NULL) { COPPERPLATE_UNPROTECT(svc); return -ENOMEM; } strncpy(ccb->name, name, sizeof(ccb->name)); ccb->name[sizeof(ccb->name) - 1] = '\0'; ccb->nwaiters = 0; if (cluster_addobj(&alchemy_cond_table, ccb->name, &ccb->cobj)) { xnfree(ccb); COPPERPLATE_UNPROTECT(svc); return -EEXIST; } __RT(pthread_mutexattr_init(&mattr)); __RT(pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT)); __RT(pthread_mutexattr_setpshared(&mattr, mutex_scope_attribute)); __RT(pthread_mutex_init(&ccb->safe, &mattr)); __RT(pthread_mutexattr_destroy(&mattr)); __RT(pthread_condattr_init(&cattr)); __RT(pthread_condattr_setpshared(&cattr, mutex_scope_attribute)); __RT(pthread_condattr_setclock(&cattr, CLOCK_COPPERPLATE)); __RT(pthread_cond_init(&ccb->cond, &cattr)); __RT(pthread_condattr_destroy(&cattr)); ccb->magic = cond_magic; cond->handle = mainheap_ref(ccb, uintptr_t); COPPERPLATE_UNPROTECT(svc); return 0; }
void CThreadEvent::_init(bool initial) { #ifdef _WINDOWS m_hEvent = CreateEvent( NULL, TRUE, initial ? TRUE : FALSE, NULL ); if( m_hEvent == NULL ) { MessageBox( NULL, "CreateEvent failed for some reason!", "Critical Failure", MB_OK|MB_ICONERROR ); } #else pthread_condattr_t ca; CHECK_UNIX_ERROR(pthread_condattr_init(&ca)); CHECK_UNIX_ERROR(pthread_condattr_setpshared(&ca, PTHREAD_PROCESS_PRIVATE)); CHECK_UNIX_ERROR(pthread_cond_init(&m_cond, &ca)); CHECK_UNIX_ERROR(pthread_condattr_destroy(&ca)); m_signaled = initial; #endif }
void init_sync_objects_in_shared_memory(shdata *data) { pthread_mutexattr_t mattr; pthread_mutexattr_init(&mattr); pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); pthread_mutex_init(&data[0].mut, &mattr); pthread_mutex_init(&data[0].mut2, &mattr); pthread_mutex_init(&data[0].tablemut, &mattr); pthread_mutex_init(&data[0].logmut, &mattr); pthread_condattr_t cattr; pthread_condattr_init(&cattr); pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED); pthread_cond_init(&data[0].cvar, &cattr); pthread_cond_init(&data[0].cvar2, &cattr); pthread_cond_init(&data[0].ctable, &cattr); pthread_cond_init(&data[0].clog, &cattr); }
/** * Initialize the memory with our memory_layout struct at the very beginning */ memory_layout *shared_mem_init(char* mem_base) { memset(mem_base, 0, sizeof(memory_layout)); memory_layout *layout = (memory_layout *) mem_base; layout->next_free = mem_base + sizeof(memory_layout); layout->open_nodes = NULL; layout->processes = NULL; layout->resources = NULL; pthread_mutexattr_init(&layout->mutexattr); pthread_mutexattr_setpshared(&layout->mutexattr, PTHREAD_PROCESS_SHARED); pthread_mutex_init(&layout->mutex, &layout->mutexattr); pthread_condattr_init(&layout->condattr); pthread_condattr_setpshared(&layout->condattr, PTHREAD_PROCESS_SHARED); pthread_cond_init(&layout->no_cycle, &layout->condattr); return layout; }