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 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; int pshared; /* Initialize a mutex attributes object */ if (pthread_condattr_init(&attr) != 0) { perror("Error at pthread_condattr_init()\n"); return PTS_UNRESOLVED; } /* The default 'pshared' attribute should be PTHREAD_PROCESS_PRIVATE */ if (pthread_condattr_getpshared(&attr, &pshared) != 0) { fprintf(stderr,"Error obtaining the attribute process-shared\n"); return PTS_UNRESOLVED; } if (pshared != PTHREAD_PROCESS_PRIVATE) { printf("Test FAILED: Incorrect default pshared value: %d\n", pshared); return PTS_FAIL; } printf("Test PASSED\n"); return PTS_PASS; }
static int my_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) { pthread_cond_t *realcond = NULL; int pshared = 0; if (attr) pthread_condattr_getpshared(attr, &pshared); if (!pshared) { /* non shared, standard cond: use malloc */ realcond = malloc(sizeof(pthread_cond_t)); *((unsigned int *) cond) = (unsigned int) realcond; } else { /* process-shared condition: use the shared memory segment */ hybris_shm_pointer_t handle = hybris_shm_alloc(sizeof(pthread_cond_t)); *((unsigned int *)cond) = (unsigned int) handle; if (handle) realcond = (pthread_cond_t *)hybris_get_shmpointer(handle); } return pthread_cond_init(realcond, attr); }
static int check (pthread_condattr_t *condattr, int pshared, clockid_t cl) { clockid_t cl2; if (pthread_condattr_getclock (condattr, &cl2) != 0) { puts ("condattr_getclock failed"); return 1; } if (cl != cl2) { printf ("condattr_getclock returned wrong value: %d, expected %d\n", (int) cl2, (int) cl); return 1; } int p; if (pthread_condattr_getpshared (condattr, &p) != 0) { puts ("condattr_getpshared failed"); return 1; } else if (p != pshared) { printf ("condattr_getpshared returned wrong value: %d, expected %d\n", p, pshared); return 1; } return 0; }
void test( void ) { pthread_condattr_t attribute; int pshared; int result; result = pthread_condattr_getpshared( &attribute, &pshared ); }
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; }
TEST(pthread, pthread_condattr_init) { pthread_condattr_t attr; pthread_condattr_init(&attr); clockid_t clock; ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); ASSERT_EQ(CLOCK_REALTIME, clock); int pshared; ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared); }
static void print_shared(pthread_condattr_t *attr) { int rc; int shared; char *s; rc = pthread_condattr_getpshared( attr, &shared ); assert( rc == 0 ); switch ( shared ) { case PTHREAD_PROCESS_PRIVATE: s = "PTHREAD_PROCESS_PRIVATE"; break; case PTHREAD_PROCESS_SHARED: s = "PTHREAD_PROCESS_SHARED"; break; default: s = "UNKNOWN"; break; } printf( "Process shared: %s\n", s ); }
static int print_condattr(const pthread_condattr_t *cond_attr) { if (cond_attr == NULL) { return THREAD_ATTR_FAILED; } int ret = 0; int pshare = 0; ret = pthread_condattr_getpshared(cond_attr, &pshare); if (pshare == PTHREAD_PROCESS_PRIVATE) { printf("cond shared: PRIVATE\n"); } else if (pshare == PTHREAD_PROCESS_SHARED) { printf("cond shared: SHARED\n"); } return THREAD_ATTR_OK; }
void *POSIX_Init( void *argument ) { int status; pthread_condattr_t attr; pthread_condattr_t attr_error; int pshared; pthread_cond_t cond; struct timespec timeout; puts( "\n\n*** POSIX TEST 10 ***" ); puts( "Init: pthread_condattr_init" ); status = pthread_condattr_init( &attr ); rtems_test_assert( !status ); puts( "Init: pthread_condattr_init - EINVAL (attribute invalid)" ); status = pthread_condattr_init( NULL ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_condattr_destroy" ); status = pthread_condattr_destroy( &attr ); rtems_test_assert( !status ); puts( "Init: pthread_condattr_destroy - EINVAL (attribute invalid)" ); status = pthread_condattr_destroy( NULL ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_condattr_init" ); status = pthread_condattr_init( &attr ); rtems_test_assert( !status ); puts( "Init: pthread_condattr_setpshared - PTHREAD_PROCESS_SHARED" ); status = pthread_condattr_setpshared( &attr, PTHREAD_PROCESS_SHARED ); rtems_test_assert( !status ); puts( "Init: pthread_condattr_setpshared - PTHREAD_PROCESS_PRIVATE" ); status = pthread_condattr_setpshared( &attr, PTHREAD_PROCESS_PRIVATE ); rtems_test_assert( !status ); status = pthread_condattr_setpshared( NULL, PTHREAD_PROCESS_PRIVATE ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_condattr_setpshared - EINVAL (attribute invalid)" ); status = pthread_condattr_setpshared( &attr, 0x7FFF ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_condattr_setpshared - EINVAL (pshared invalid)" ); status = pthread_condattr_getpshared( &attr, &pshared ); rtems_test_assert( !status ); printf( "Init: pthread_condattr_getpshared - %d\n", pshared ); status = pthread_condattr_getpshared( NULL, &pshared ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_condattr_getpshared - EINVAL (attribute invalid)" ); puts( "Init: pthread_cond_init - NULL attr" ); status = pthread_cond_init( &cond, NULL ); rtems_test_assert( !status ); /* error for attribute not initialized */ attr_error.is_initialized = FALSE; status = pthread_cond_init( &cond, &attr_error ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_cond_init - EINVAL (attr not initialized)" ); status = pthread_cond_init( &cond, NULL ); if ( status != ENOMEM ) printf( "status = %d\n", status ); rtems_test_assert( status == ENOMEM ); puts( "Init: pthread_cond_init - ENOMEM (too many conds)" ); puts( "Init: pthread_cond_destroy" ); status = pthread_cond_destroy( &cond ); rtems_test_assert( !status ); /* error for bad condition variable passed */ status = pthread_cond_destroy( NULL ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_cond_destroy - EINVAL (cond invalid)" ); /* initiailize the attribute for the rest of the test */ puts( "Init: pthread_cond_init - attr" ); status = pthread_cond_init( &Cond1_id, &attr ); rtems_test_assert( !status ); /* signal task1 with a condition variable */ empty_line(); status = pthread_create( &Task_id, NULL, Task_1, NULL ); rtems_test_assert( !status ); /* switch to task1 to allow it to wait for a condition variable */ puts( "Init: sleep to switch to Task_1" ); sleep( 1 ); status = pthread_cond_destroy( &Cond1_id ); if ( status != EBUSY ) printf( "status = %d\n", status ); rtems_test_assert( status == EBUSY ); puts( "Init: pthread_cond_destroy - EBUSY (task1 waiting)" ); puts( "Init: pthread_cond_signal" ); status = pthread_cond_signal( &Cond1_id ); rtems_test_assert( !status ); empty_line(); status = pthread_create( &Task2_id, NULL, Task_2, NULL ); rtems_test_assert( !status ); /* switch to task1 and task2 to allow them to wait for broadcast signal */ puts( "Init: sleep - switch to Task_1 and Task_2" ); sleep( 1 ); /* broadcast a condition variable to task1 and task2 */ puts( "Init: pthread_cond_broadcast" ); status = pthread_cond_broadcast( &Cond1_id ); rtems_test_assert( !status ); puts( "Init: sleep - switch to Task_1" ); sleep( 0 ); /* timedwait case - timeout */ status = pthread_mutex_lock( &Mutex_id ); rtems_test_assert( !status ); /* set timeout to 3 seconds */ status = clock_gettime( CLOCK_REALTIME, &timeout ); rtems_test_assert( !status ); timeout.tv_sec += 3; timeout.tv_nsec = 0; puts( "Init: pthread_cond_timedwait for 3 seconds" ); status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); if ( status != ETIMEDOUT ) printf( "status = %d\n", status ); rtems_test_assert( status == ETIMEDOUT ); puts( "Init: pthread_cond_timedwait - ETIMEDOUT - (mutex not acquired)" ); status = pthread_mutex_unlock( &Mutex_id ); rtems_test_assert( !status ); /* remaining error messages */ empty_line(); /* errors for bad variable passed */ status = pthread_cond_signal( NULL ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_cond_signal - EINVAL (cond invalid)" ); status = pthread_cond_broadcast( NULL ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_cond_broadcast - EINVAL (cond invalid)" ); /* acquire mutex so errors will occur */ status = pthread_mutex_lock( &Mutex_id ); rtems_test_assert( !status ); status = pthread_cond_wait( NULL, &Mutex_id ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_cond_wait - EINVAL (cond invalid)" ); status = pthread_cond_timedwait( NULL, &Mutex_id, &timeout ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_cond_timedwait - EINVAL (cond invalid)" ); status = pthread_cond_wait( &Cond1_id, NULL ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_cond_wait - EINVAL (mutex invalid)" ); status = pthread_cond_timedwait( &Cond1_id, NULL, &timeout ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_cond_timedwait - EINVAL (mutex invalid)" ); status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, NULL ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_cond_timedwait - EINVAL (abstime NULL)" ); status = clock_gettime( CLOCK_REALTIME, &timeout ); rtems_test_assert( !status ); timeout.tv_sec -= 1; status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); if ( status != ETIMEDOUT ) printf( "status = %d\n", status ); rtems_test_assert( status == ETIMEDOUT ); puts( "Init: pthread_cond_timedwait - ETIMEDOUT (abstime->tv_sec < current time)" ); status = pthread_mutex_unlock( &Mutex_id ); rtems_test_assert( !status ); status = pthread_mutex_lock( &Mutex_id ); rtems_test_assert( !status ); /* ensure we do not catch a 0 nanosecond boundary */ do { status = clock_gettime( CLOCK_REALTIME, &timeout ); rtems_test_assert( !status ); timeout.tv_nsec -= 1; } while ( timeout.tv_nsec < 0); status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); if ( status != ETIMEDOUT ) printf( "status = %d\n", status ); rtems_test_assert( status == ETIMEDOUT ); puts( "Init: pthread_cond_timedwait - ETIMEDOUT (abstime->tv_nsec < current time)" ); status = pthread_mutex_unlock( &Mutex_id ); rtems_test_assert( !status ); /* wait and timedwait without mutex */ /* XXX - this case is commented out in the code pending review * * status = pthread_cond_wait( &Cond1_id, &Mutex_id ); * if ( status != EINVAL ) * printf( "status = %d\n", status ); * rtems_test_assert( status == EINVAL ); */ puts( "Init: pthread_cond_wait - EINVAL (mutex not locked before call)" ); /* XXX - this case is commented out in the code pending review * * status = clock_gettime( CLOCK_REALTIME, &timeout ); * rtems_test_assert( !status ); * timeout.tv_sec += 1; * status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); * if ( status != EINVAL ) * printf( "status = %d\n", status ); * rtems_test_assert( status == EINVAL ); */ puts( "Init: pthread_cond_timedwait - EINVAL (mutex not locked before call)"); empty_line(); status = pthread_create( &Task3_id, NULL, Task_3, NULL ); rtems_test_assert( !status ); /* switch to task3 to allow it to wait for broadcast signal */ puts( "Init: sleep - switch to Task_3" ); sleep( 1 ); /* destroy the mutex so Task3 can not acguire at the end of Wait_support */ status = pthread_mutex_destroy( &Mutex_id ); rtems_test_assert( !status ); /* signal a condition variable to task3 */ puts( "Init: pthread_cond_signal" ); status = pthread_cond_signal( &Cond1_id ); puts( "Init: sleep - switch to Task_3" ); sleep( 1 ); puts( "*** END OF POSIX TEST 10 ***" ); rtems_test_exit( 0 ); return NULL; /* just so the compiler thinks we returned something */ }