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_mutexattr_t mta; int pshared; /* Initialize a mutex attributes object */ if (pthread_mutexattr_init(&mta) != 0) { perror("Error at pthread_mutexattr_init()\n"); return PTS_UNRESOLVED; } /* The default 'pshared' attribute should be PTHREAD_PROCESS_PRIVATE */ if (pthread_mutexattr_getpshared(&mta, &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; }
pshared_t MutexAttr::GetPShared() { int result; if(pthread_mutexattr_getpshared(&m_MutexAttr,&result)!=0) throw std::runtime_error("pthread_mutexattr_getpshared failed"); return pshared_t(result); }
static int my_pthread_mutex_init(pthread_mutex_t *__mutex, __const pthread_mutexattr_t *__mutexattr) { pthread_mutex_t *realmutex = NULL; int pshared = 0; if (__mutexattr) pthread_mutexattr_getpshared(__mutexattr, &pshared); if (!pshared) { /* non shared, standard mutex: use malloc */ realmutex = malloc(sizeof(pthread_mutex_t)); *((unsigned int *)__mutex) = (unsigned int) realmutex; } else { /* process-shared mutex: use the shared memory segment */ hybris_shm_pointer_t handle = hybris_shm_alloc(sizeof(pthread_mutex_t)); *((hybris_shm_pointer_t *)__mutex) = handle; if (handle) realmutex = (pthread_mutex_t *)hybris_get_shmpointer(handle); } return pthread_mutex_init(realmutex, __mutexattr); }
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_mutexattr_t mta; int ret; int pshared; /* Pass the uninitialized and invalid 'attr' object.*/ memset(&mta, 0, sizeof(mta)); /* The default 'pshared' attribute should be PTHREAD_PROCESS_PRIVATE */ ret=pthread_mutexattr_getpshared(&mta, &pshared); if (ret != 0) { if (ret == EINVAL) { printf("Test PASSED\n"); return PTS_PASS; } printf("Test FAILED: Incorrect return code. Expected 0 or EINVAL, got: %d\n", ret); return PTS_FAIL; } printf("Test PASSED: NOTE*: Returned 0 on error, though standard states 'may' fail.\n"); return PTS_PASS; }
int pthread_mutex_init (pthread_mutex_t *m, const pthread_mutexattr_t *a) { mutex_t *_m; int r = mutex_ref_init (m); if (r) return r; if (!(_m = (pthread_mutex_t)calloc(1,sizeof(*_m)))) { pthread_spin_unlock (&mutex_global); return ENOMEM; } _m->type = PTHREAD_MUTEX_DEFAULT; _m->count = 0; _m->busy = 0; if (a) { int share = PTHREAD_PROCESS_PRIVATE; r = pthread_mutexattr_gettype (a, &_m->type); if (!r) r = pthread_mutexattr_getpshared(a, &share); if (!r && share == PTHREAD_PROCESS_SHARED) r = ENOSYS; } if (!r) { if ((_m->h = CreateSemaphore(NULL, 1, 0x7fffffff, NULL)) == NULL) { switch (GetLastError()) { case ERROR_ACCESS_DENIED: r = EPERM; break; default: /* We assume this, to keep it simple: */ r = ENOMEM; } } } if (r) { _m->valid = DEAD_MUTEX; free(_m); *m = NULL; pthread_spin_unlock (&mutex_global); return r; } _m->valid = LIFE_MUTEX; *m = _m; pthread_spin_unlock (&mutex_global); return 0; }
/*! Get the state of the shared flag \return bool \retval true - mutex can be used to synchronize processes. \retval false - Mutex can only be used within the process. */ bool CMutexAttr::isShareable() { int value; int status = pthread_mutexattr_getpshared(&m_attributes, &value); throwifbad(status, "CMutexAttr::isShareable"); return value == PTHREAD_PROCESS_SHARED; }
void test( void ) { pthread_mutexattr_t attribute; int pshared; int result; result = pthread_mutexattr_getpshared( &attribute, &pshared ); }
void Print_mutexattr( char *msg, pthread_mutexattr_t *attr ) { int status; int protocol; int prioceiling; int pshared; /* protocol */ status = pthread_mutexattr_getprotocol( attr, &protocol ); rtems_test_assert( !status ); printf( "%smutex protocol is (%d) -- ", msg, protocol ); switch ( protocol ) { case PTHREAD_PRIO_NONE: puts( "PTHREAD_PRIO_NONE" ); break; case PTHREAD_PRIO_INHERIT: puts( "PTHREAD_PRIO_INHERIT" ); break; case PTHREAD_PRIO_PROTECT: puts( "PTHREAD_PRIO_PROTECT" ); break; default: puts( "UNKNOWN" ); rtems_test_assert( 0 ); break; } /* priority ceiling */ status = pthread_mutexattr_getprioceiling( attr, &prioceiling ); rtems_test_assert( !status ); printf( "%smutex priority ceiling is %d\n", msg, prioceiling ); /* process shared */ status = pthread_mutexattr_getpshared( attr, &pshared ); rtems_test_assert( !status ); printf( "%smutex process shared is (%d) -- ", msg, pshared ); switch ( pshared ) { case PTHREAD_PROCESS_PRIVATE: puts( "PTHREAD_PROCESS_PRIVATE" ); break; case PTHREAD_PROCESS_SHARED: puts( "PTHREAD_PROCESS_SHARED" ); break; default: puts( "UNKNOWN" ); rtems_test_assert( 0 ); break; } }
Mutex::Mutex(bool isprocess_shared): process_shared(isprocess_shared) { pthread_mutexattr_t arrr_t; pthread_mutexattr_settype(&arrr_t,PTHREAD_MUTEX_NORMAL); if( process_shared ) { int shared; pthread_mutexattr_getpshared(&arrr_t, &shared); assert(shared == PTHREAD_PROCESS_PRIVATE); pthread_mutexattr_setpshared(&arrr_t, PTHREAD_PROCESS_SHARED); } pthread_mutex_init(&m_mutex,&arrr_t); }
int cobalt_mutexattr_getpshared(const pthread_mutexattr_t __user *u_attr, int __user *u_pshared) { pthread_mutexattr_t attr; int err, pshared; if (__xn_safe_copy_from_user(&attr, u_attr, sizeof(attr))) return -EFAULT; err = pthread_mutexattr_getpshared(&attr, &pshared); if (err) return -err; return __xn_safe_copy_to_user(u_pshared, &pshared, sizeof(*u_pshared)); }
static int print_mutexattr(const pthread_mutexattr_t *mutex_attr) { if (mutex_attr == NULL) { return THREAD_ATTR_FAILED; } int ret = 0; int pshare = 0; int type = 0; ret = pthread_mutexattr_getpshared(mutex_attr, &pshare); if (pshare == PTHREAD_PROCESS_PRIVATE) { printf("mutex shared: PRIVATE\n"); } else if (pshare == PTHREAD_PROCESS_SHARED) { printf("mutex shared: SHARED\n"); } ret = pthread_mutexattr_gettype(mutex_attr, &type); if (type == PTHREAD_MUTEX_NORMAL) { printf("mutex NORMAL\n"); } else if (type == PTHREAD_MUTEX_ERRORCHECK) { printf("mutex ERRORCHECK\n"); } else if (type == PTHREAD_MUTEX_RECURSIVE) { printf("mutex RECURSIVE\n"); } else if (type == PTHREAD_MUTEX_DEFAULT) { printf("mutex DEFAULT\n"); } return THREAD_ATTR_OK; }
int main() { pthread_mutexattr_t mta; int rc; #ifdef PTHREAD_PROCESS_SHARED int pshared; #endif /* Initialize a mutex attributes object */ if((rc=pthread_mutexattr_init(&mta)) != 0) { fprintf(stderr,"Error at pthread_mutexattr_init(), rc=%d\n",rc); printf("Test FAILED\n"); return PTS_FAIL; } #ifdef PTHREAD_PROCESS_SHARED /* If the symbol {PTHREAD_PROCESS_SHARED} is defined, the attribute * process-shared should be provided and its default value should be * PTHREAD_PROCESS_PRIVATE */ if(pthread_mutexattr_getpshared(&mta, &pshared) != 0) { fprintf(stderr,"Error obtaining the attribute process-shared\n"); return PTS_UNRESOLVED; } if(pshared == PTHREAD_PROCESS_PRIVATE) { printf("Test PASSED\n"); return PTS_PASS; } else { printf("Test FAILED\n"); return PTS_FAIL; } #endif fprintf(stderr,"process-shared attribute is not available for testing\n"); return PTS_UNRESOLVED; }
int main() { int fd; unsigned int val; struct mydata *addr; pthread_mutexattr_t pshared; pthread_mutexattr_init(&pshared); pthread_mutexattr_setpshared(&pshared, PTHREAD_PROCESS_SHARED); pthread_mutexattr_getpshared(&pshared , &val); printf(" %d\n", val); fd = shm_open(DATA, O_CREAT | O_RDWR, 666); if (fd == -1) { perror("shm_open"); exit(1); } /* 4k is min shared memory */ if (ftruncate(fd, getpagesize()) == -1) { perror("ftruncate"); exit(1); } addr = (struct mydata *)mmap(NULL, sizeof(struct mydata), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (addr == MAP_FAILED) { perror("mmap"); exit(1); } pthread_mutex_init(&addr->mtxlock, &pshared); addr->a = 0; addr->b = 0; munmap(addr, sizeof(struct mydata)); return 0; }
int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *a) { mutex_t *_m; int r = mutex_ref_init(m); if(r) return r; if (!(_m = (pthread_mutex_t)calloc(1,sizeof(*_m)))) return ENOMEM; _m->type = PTHREAD_MUTEX_DEFAULT; _m->count = 0; if (a) { int share = PTHREAD_PROCESS_SHARED; r = pthread_mutexattr_gettype(a, &_m->type); if (!r) r = pthread_mutexattr_getpshared(a, &share); if (!r && share == PTHREAD_PROCESS_SHARED) r = ENOSYS; } if (!r) { #if defined USE_MUTEX_Mutex if ( (_m->h = CreateSemaphore(NULL, 1, 0x7fffffff, NULL)) != NULL) { #else /* USE_MUTEX_CriticalSection */ if (InitializeCriticalSectionAndSpinCount(&_m->cs.cs, USE_MUTEX_CriticalSection_SpinCount)) { #endif } else { #if defined USE_MUTEX_Mutex switch (GetLastError()) { case ERROR_ACCESS_DENIED: r = EPERM; break; default: /* We assume this, to keep it simple: */ r = ENOMEM; } #else /* USE_MUTEX_CriticalSection */ r = ENOMEM; #endif } } if (r) { _m->valid = DEAD_MUTEX; free(_m); *m = NULL; return r; } if (InterlockedExchange(&InitOnce, 0)) _mutex_init_once(_m); _m->valid = LIFE_MUTEX; *m = _m; return 0; } int pthread_mutex_destroy(pthread_mutex_t *m) { pthread_mutex_t mDestroy; int r = mutex_ref_destroy(m,&mDestroy); if(r) return r; if(!mDestroy) return 0; /* destroyed a (still) static initialized mutex */ /* now the mutex is invalid, and no one can touch it */ mutex_t *_m = (mutex_t *)mDestroy; #if defined USE_MUTEX_Mutex CloseHandle(_m->h); #else /* USE_MUTEX_CriticalSection */ DeleteCriticalSection(&_m->cs.cs); #endif _m->valid = DEAD_MUTEX; _m->type = 0; _m->count = 0; free(mDestroy); return 0; }
int pthread_mutexattr_test(){ pthread_mutexattr_t attr; int tmp; int i; printf("Test pthread_mutexattr_init()..."); if ( pthread_mutexattr_init(&attr) < 0 ){ fflush(stdout); perror("failed"); return -1; } printf("passed\n"); printf("Test pthread_mutexattr_set/getprioceiling()..."); for(i = sched_get_priority_min(SCHED_OTHER); i <= sched_get_priority_max(SCHED_OTHER); i++ ){ if ( pthread_mutexattr_setprioceiling(&attr, i) < 0 ){ fflush(stdout); perror("failed"); return -1; } if ( pthread_mutexattr_getprioceiling(&attr, &tmp) < 0 ){ fflush(stdout); perror("failed"); return -1; } if ( tmp != i ){ printf("failed to set/get (%d != %d)\n", tmp, i); return -1; } } printf("passed\n"); if ( set_get_test("pthread_mutexattr_set/getprotocol()", (int (*)(void*,int))pthread_mutexattr_setprotocol, (int (*)(void*,int*))pthread_mutexattr_getprotocol, &attr, protocols, PROTOCOLS_TOTAL, 0) ){ return -1; } printf("Test pthread_mutexattr_set/getpshared()..."); tmp = 1; if ( pthread_mutexattr_setpshared(&attr, tmp) < 0 ){ fflush(stdout); perror("failed"); return -1; } if ( pthread_mutexattr_getpshared(&attr, &tmp) < 0 ){ fflush(stdout); perror("failed"); return -1; } if ( tmp != 1 ){ fflush(stdout); perror("failed"); return -1; } printf("passed\n"); if ( set_get_test("pthread_mutexattr_set/gettype()", (int (*)(void*,int))pthread_mutexattr_settype, (int (*)(void*,int*))pthread_mutexattr_gettype, &attr, types, TYPES_TOTAL, 0) ){ return -1; } printf("Test pthread_mutexattr_destroy()..."); if ( pthread_mutexattr_destroy(&attr) < 0 ){ fflush(stdout); perror("failed"); return -1; } printf("passed\n"); printf("Stress Test pthread_mutexattr_destroy()..."); if ( pthread_mutexattr_destroy(&attr) == 0 ){ printf("should have failed\n"); return -1; } errno = 0; printf("passed\n"); return 0; }
static int do_test (void) { size_t ps = sysconf (_SC_PAGESIZE); char tmpfname[] = "/tmp/tst-mutex4.XXXXXX"; char data[ps]; void *mem; int fd; pthread_mutex_t *m; pthread_mutexattr_t a; pid_t pid; char *p; int err; int s; fd = mkstemp (tmpfname); if (fd == -1) { printf ("cannot open temporary file: %m\n"); return 1; } /* Make sure it is always removed. */ unlink (tmpfname); /* Create one page of data. */ memset (data, '\0', ps); /* Write the data to the file. */ if (write (fd, data, ps) != (ssize_t) ps) { puts ("short write"); return 1; } mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (mem == MAP_FAILED) { printf ("mmap failed: %m\n"); return 1; } m = (pthread_mutex_t *) (((uintptr_t) mem + __alignof (pthread_mutex_t)) & ~(__alignof (pthread_mutex_t) - 1)); p = (char *) (m + 1); if (pthread_mutexattr_init (&a) != 0) { puts ("mutexattr_init failed"); return 1; } if (pthread_mutexattr_getpshared (&a, &s) != 0) { puts ("1st mutexattr_getpshared failed"); return 1; } if (s != PTHREAD_PROCESS_PRIVATE) { puts ("default pshared value wrong"); return 1; } if (pthread_mutexattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0) { puts ("mutexattr_setpshared failed"); return 1; } if (pthread_mutexattr_getpshared (&a, &s) != 0) { puts ("2nd mutexattr_getpshared failed"); return 1; } if (s != PTHREAD_PROCESS_SHARED) { puts ("pshared value after setpshared call wrong"); return 1; } if (pthread_mutex_init (m, &a) != 0) { puts ("mutex_init failed"); return 1; } if (pthread_mutex_lock (m) != 0) { puts ("mutex_lock failed"); return 1; } if (pthread_mutexattr_destroy (&a) != 0) { puts ("mutexattr_destroy failed"); return 1; } err = pthread_mutex_trylock (m); if (err == 0) { puts ("mutex_trylock succeeded"); return 1; } else if (err != EBUSY) { puts ("mutex_trylock didn't return EBUSY"); return 1; } *p = 0; puts ("going to fork now"); pid = fork (); if (pid == -1) { puts ("fork failed"); return 1; } else if (pid == 0) { /* Play some lock ping-pong. It's our turn to unlock first. */ if ((*p)++ != 0) { puts ("child: *p != 0"); return 1; } if (pthread_mutex_unlock (m) != 0) { puts ("child: 1st mutex_unlock failed"); return 1; } puts ("child done"); } else { if (pthread_mutex_lock (m) != 0) { puts ("parent: 2nd mutex_lock failed"); return 1; } if (*p != 1) { puts ("*p != 1"); return 1; } puts ("parent done"); } return 0; }
static int do_test (void) { size_t ps = sysconf (_SC_PAGESIZE); char tmpfname[] = "/tmp/tst-mutex4.XXXXXX"; char data[ps]; void *mem; int fd; pthread_mutex_t *m; pthread_mutexattr_t a; pid_t pid; char *p; int err; int s; pthread_barrier_t *b; pthread_barrierattr_t ba; fd = mkstemp (tmpfname); if (fd == -1) { printf ("cannot open temporary file: %m\n"); return 1; } /* Make sure it is always removed. */ unlink (tmpfname); /* Create one page of data. */ memset (data, '\0', ps); /* Write the data to the file. */ if (write (fd, data, ps) != (ssize_t) ps) { puts ("short write"); return 1; } mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (mem == MAP_FAILED) { printf ("mmap failed: %m\n"); return 1; } m = (pthread_mutex_t *) (((uintptr_t) mem + __alignof (pthread_mutex_t) - 1) & ~(__alignof (pthread_mutex_t) - 1)); b = (pthread_barrier_t *) (((uintptr_t) (m + 1) + __alignof (pthread_barrier_t) - 1) & ~(__alignof (pthread_barrier_t) - 1)); p = (char *) (b + 1); if (pthread_mutexattr_init (&a) != 0) { puts ("mutexattr_init failed"); return 1; } if (pthread_mutexattr_getpshared (&a, &s) != 0) { puts ("1st mutexattr_getpshared failed"); return 1; } if (s != PTHREAD_PROCESS_PRIVATE) { puts ("default pshared value wrong"); return 1; } if (pthread_mutexattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0) { puts ("mutexattr_setpshared failed"); return 1; } if (pthread_mutexattr_getpshared (&a, &s) != 0) { puts ("2nd mutexattr_getpshared failed"); return 1; } if (s != PTHREAD_PROCESS_SHARED) { puts ("pshared value after setpshared call wrong"); return 1; } #ifdef ENABLE_PI if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0) { puts ("pthread_mutexattr_setprotocol failed"); return 1; } #endif if ((err = pthread_mutex_init (m, &a)) != 0) { #ifdef ENABLE_PI if (err == ENOTSUP) { puts ("PI mutexes unsupported"); return 0; } #endif puts ("mutex_init failed"); return 1; } if (pthread_mutex_lock (m) != 0) { puts ("mutex_lock failed"); return 1; } if (pthread_mutexattr_destroy (&a) != 0) { puts ("mutexattr_destroy failed"); return 1; } if (pthread_barrierattr_init (&ba) != 0) { puts ("barrierattr_init failed"); return 1; } if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0) { puts ("barrierattr_setpshared failed"); return 1; } if (pthread_barrier_init (b, &ba, 2) != 0) { puts ("barrier_init failed"); return 1; } if (pthread_barrierattr_destroy (&ba) != 0) { puts ("barrierattr_destroy failed"); return 1; } err = pthread_mutex_trylock (m); if (err == 0) { puts ("mutex_trylock succeeded"); return 1; } else if (err != EBUSY) { puts ("mutex_trylock didn't return EBUSY"); return 1; } *p = 0; if (pthread_mutex_unlock (m) != 0) { puts ("parent: 1st mutex_unlock failed"); return 1; } puts ("going to fork now"); pid = fork (); if (pid == -1) { puts ("fork failed"); return 1; } else if (pid == 0) { if (pthread_mutex_lock (m) != 0) { puts ("child: mutex_lock failed"); return 1; } int e = pthread_barrier_wait (b); if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) { puts ("child: barrier_wait failed"); return 1; } if ((*p)++ != 0) { puts ("child: *p != 0"); return 1; } if (pthread_mutex_unlock (m) != 0) { puts ("child: mutex_unlock failed"); return 1; } puts ("child done"); } else { int e = pthread_barrier_wait (b); if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) { puts ("parent: barrier_wait failed"); return 1; } if (pthread_mutex_lock (m) != 0) { puts ("parent: 2nd mutex_lock failed"); return 1; } if (*p != 1) { puts ("*p != 1"); return 1; } if (pthread_mutex_unlock (m) != 0) { puts ("parent: 2nd mutex_unlock failed"); return 1; } if (pthread_mutex_destroy (m) != 0) { puts ("mutex_destroy failed"); return 1; } if (pthread_barrier_destroy (b) != 0) { puts ("barrier_destroy failed"); return 1; } puts ("parent done"); } return 0; }
void *POSIX_Init( void *argument ) { int status; pthread_mutexattr_t attr; pthread_mutexattr_t destroyed_attr; struct timespec times; struct sched_param param; int pshared; int policy; int protocol; int ceiling; int old_ceiling; int priority; rtems_test_assert( MUTEX_BAD_ID != PTHREAD_MUTEX_INITIALIZER ); Mutex_bad_id = MUTEX_BAD_ID; TEST_BEGIN(); /* set the time of day, and print our buffer in multiple ways */ set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 ); /* get id of this thread */ Init_id = pthread_self(); printf( "Init's ID is 0x%08" PRIxpthread_t "\n", Init_id ); /* test pthread_mutex_attr_init */ puts( "Init: pthread_mutexattr_init - EINVAL (NULL attr)" ); status = pthread_mutexattr_init( NULL ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); status = pthread_mutexattr_init( &attr ); rtems_test_assert( !status ); Print_mutexattr( "Init: ", &attr ); /* create an "uninitialized" attribute structure */ status = pthread_mutexattr_init( &destroyed_attr ); rtems_test_assert( !status ); puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" ); status = pthread_mutexattr_destroy( &destroyed_attr ); rtems_test_assert( !status ); puts( "Init: pthread_mutexattr_destroy - EINVAL (NULL attr)" ); status = pthread_mutexattr_destroy( NULL ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutexattr_destroy - EINVAL (not initialized)" ); status = pthread_mutexattr_destroy( &destroyed_attr ); rtems_test_assert( status == EINVAL ); /* error cases for set and get pshared attribute */ empty_line(); puts( "Init: pthread_mutexattr_getpshared - EINVAL (NULL attr)" ); status = pthread_mutexattr_getpshared( NULL, &pshared ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutexattr_getpshared - EINVAL (NULL pshared)" ); status = pthread_mutexattr_getpshared( &attr, NULL ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutexattr_getpshared - EINVAL (not initialized)" ); status = pthread_mutexattr_getpshared( &destroyed_attr, &pshared ); rtems_test_assert( status == EINVAL ); pshared = PTHREAD_PROCESS_PRIVATE; puts( "Init: pthread_mutexattr_setpshared - EINVAL (NULL attr)" ); status = pthread_mutexattr_setpshared( NULL, pshared ); rtems_test_assert( status == EINVAL ); pshared = PTHREAD_PROCESS_PRIVATE; puts( "Init: pthread_mutexattr_setpshared - EINVAL (not initialized)" ); status = pthread_mutexattr_setpshared( &destroyed_attr, pshared ); rtems_test_assert( status == EINVAL ); /* error cases for set and get protocol attribute */ empty_line(); puts( "Init: pthread_mutexattr_getprotocol - EINVAL (NULL attr)" ); status = pthread_mutexattr_getprotocol( NULL, &protocol ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutexattr_getprotocol - EINVAL (NULL protocol)" ); status = pthread_mutexattr_getprotocol( &attr, NULL ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutexattr_getprotocol - EINVAL (not initialized)" ); status = pthread_mutexattr_getprotocol( &destroyed_attr, &protocol ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutexattr_setprotocol - EINVAL (NULL attr)" ); status = pthread_mutexattr_setprotocol( NULL, PTHREAD_PRIO_NONE ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutexattr_setprotocol - EINVAL (invalid protocol)" ); status = pthread_mutexattr_setprotocol( &attr, -1 ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutexattr_setprotocol - EINVAL (not initialized)" ); status = pthread_mutexattr_setprotocol( &destroyed_attr, -1 ); rtems_test_assert( status == EINVAL ); /* error cases for set and get prioceiling attribute */ empty_line(); puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (NULL attr)" ); status = pthread_mutexattr_getprioceiling( NULL, &ceiling ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (NULL prioceiling)" ); status = pthread_mutexattr_getprioceiling( &attr, NULL ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (not initialized)" ); status = pthread_mutexattr_getprioceiling( &destroyed_attr, &ceiling ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (NULL attr)" ); status = pthread_mutexattr_setprioceiling( NULL, 128 ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (invalid priority)" ); status = pthread_mutexattr_setprioceiling( &attr, 512 ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (not initialized)" ); status = pthread_mutexattr_setprioceiling( &destroyed_attr, -1 ); rtems_test_assert( status == EINVAL ); /* create a thread */ status = pthread_create( &Task_id, NULL, Task_1, NULL ); rtems_test_assert( !status ); /* now try some basic mutex operations */ empty_line(); puts( "Init: pthread_mutex_init - EINVAL (NULL mutex_id)" ); status = pthread_mutex_init( NULL, &attr ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutex_init - EINVAL (not initialized attr)" ); status = pthread_mutex_init( &Mutex_id, &destroyed_attr ); rtems_test_assert( status == EINVAL ); /* must get around error checks in attribute set routines */ attr.protocol = -1; puts( "Init: pthread_mutex_init - EINVAL (bad protocol)" ); status = pthread_mutex_init( &Mutex_id, &attr ); rtems_test_assert( status == EINVAL ); /* must get around error checks in attribute set routines */ attr.protocol = PTHREAD_PRIO_INHERIT; attr.prio_ceiling = -1; puts( "Init: pthread_mutex_init - EINVAL (bad priority ceiling)" ); status = pthread_mutex_init( &Mutex_id, &attr ); rtems_test_assert( status == EINVAL ); /* must get around various error checks before checking bad scope */ puts( "Init: Resetting mutex attributes" ); status = pthread_mutexattr_init( &attr ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_init - ENOSYS (process wide scope)" ); attr.process_shared = PTHREAD_PROCESS_SHARED; status = pthread_mutex_init( &Mutex_id, &attr ); rtems_test_assert( status == ENOSYS ); puts( "Init: pthread_mutex_init - EINVAL (invalid scope)" ); attr.process_shared = -1; status = pthread_mutex_init( &Mutex_id, &attr ); rtems_test_assert( status == EINVAL ); /* bad kind */ status = pthread_mutexattr_init( &attr ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_init - EINVAL (invalid type)" ); attr.type = -1; status = pthread_mutex_init( &Mutex_id, &attr ); rtems_test_assert( status == EINVAL ); /* now set up for a success pthread_mutex_init */ puts( "Init: Resetting mutex attributes" ); status = pthread_mutexattr_init( &attr ); rtems_test_assert( !status ); puts( "Init: Changing mutex attributes" ); status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); rtems_test_assert( !status ); status = pthread_mutexattr_setprioceiling( &attr, (sched_get_priority_max(SCHED_FIFO) / 2) + 1 ); rtems_test_assert( !status ); status = pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_SHARED ); rtems_test_assert( !status ); Print_mutexattr( "Init: ", &attr ); puts( "Init: Resetting mutex attributes" ); status = pthread_mutexattr_init( &attr ); rtems_test_assert( !status ); /* * Set the protocol to priority ceiling so the owner check happens * and the EPERM test (later) will work. */ status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_init - SUCCESSFUL" ); status = pthread_mutex_init( &Mutex_id, &attr ); if ( status ) printf( "status = %d\n", status ); rtems_test_assert( !status ); /* * This is not required to be an error and when it is, there are * behavioral conflicts with other implementations. */ puts( "Init: pthread_mutex_init - EBUSY (reinitialize an existing mutex) - skipped" ); #if 0 status = pthread_mutex_init( &Mutex_id, &attr ); if ( !status ) printf( "status = %d\n", status ); rtems_test_assert( status == EBUSY ); #endif puts( "Init: pthread_mutex_trylock - EINVAL (illegal ID)" ); status = pthread_mutex_trylock( &Mutex_bad_id ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); status = pthread_mutex_trylock( &Mutex_id ); if ( status ) printf( "status = %d\n", status ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_trylock - EDEADLK (already locked)" ); status = pthread_mutex_trylock( &Mutex_id ); if ( status != EBUSY ) printf( "status = %d\n", status ); rtems_test_assert( status == EBUSY ); puts( "Init: pthread_mutex_lock - EINVAL (NULL id)" ); status = pthread_mutex_lock( NULL ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutex_unlock - EINVAL (NULL id)" ); status = pthread_mutex_unlock( NULL ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutex_lock - EDEADLK (already locked)" ); status = pthread_mutex_lock( &Mutex_id ); if ( status != EDEADLK ) printf( "status = %d\n", status ); rtems_test_assert( status == EDEADLK ); puts( "Init: Sleep 1 second" ); sleep( 1 ); /* switch to task 1 */ puts( "Init: pthread_mutex_unlock - EINVAL (invalid id)" ); status = pthread_mutex_unlock( &Mutex_bad_id ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); status = pthread_mutex_unlock( &Mutex_id ); if ( status ) printf( "status = %d\n", status ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_unlock - EPERM (not owner)" ); status = pthread_mutex_unlock( &Mutex_id ); if ( status != EPERM ) printf( "status = %d\n", status ); rtems_test_assert( status == EPERM ); puts( "Init: pthread_mutex_timedlock - time out in 1/2 second" ); calculate_abstimeout( ×, 0, (TOD_NANOSECONDS_PER_SECOND / 2) ); status = pthread_mutex_timedlock( &Mutex_id, × ); if ( status != ETIMEDOUT ) printf( "status = %d\n", status ); rtems_test_assert( status == ETIMEDOUT ); puts( "Init: pthread_mutex_timedlock - time out in the past" ); calculate_abstimeout( ×, -1, (TOD_NANOSECONDS_PER_SECOND / 2) ); status = pthread_mutex_timedlock( &Mutex_id, × ); if ( status != ETIMEDOUT ) printf( "status = %d\n", status ); rtems_test_assert( status == ETIMEDOUT ); /* switch to idle */ puts( "Init: pthread_mutex_timedlock - EAGAIN (timeout)" ); /* destroy a mutex */ empty_line(); puts( "Init: pthread_mutex_init - SUCCESSFUL" ); status = pthread_mutex_init( &Mutex2_id, &attr ); if ( status ) printf( "status = %d\n", status ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_init - EAGAIN (too many)" ); status = pthread_mutex_init( &Mutex3_id, &attr ); rtems_test_assert( status == EAGAIN ); puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" ); status = pthread_mutexattr_destroy( &attr ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_destroy - SUCCESSFUL" ); status = pthread_mutex_destroy( &Mutex2_id ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_destroy - EINVAL (invalid id)" ); status = pthread_mutex_destroy( &Mutex_bad_id ); rtems_test_assert( status == EINVAL ); /* destroy a busy mutex */ empty_line(); puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); status = pthread_mutexattr_init( &attr ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_init - SUCCESSFUL" ); status = pthread_mutex_init( &Mutex2_id, &attr ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); status = pthread_mutex_trylock( &Mutex2_id ); if ( status ) printf( "status = %d\n", status ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_destroy - EBUSY (already locked)" ); status = pthread_mutex_destroy( &Mutex2_id ); if ( status != EBUSY ) printf( "status = %d\n", status ); rtems_test_assert( status == EBUSY ); puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); status = pthread_mutex_unlock( &Mutex2_id ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_destroy - SUCCESSFUL" ); status = pthread_mutex_destroy( &Mutex2_id ); rtems_test_assert( !status ); /* priority inherit mutex */ empty_line(); puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); status = pthread_mutexattr_init( &attr ); rtems_test_assert( !status ); puts( "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_INHERIT)" ); status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_init - SUCCESSFUL" ); status = pthread_mutex_init( &Mutex2_id, &attr ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); status = pthread_mutex_trylock( &Mutex2_id ); rtems_test_assert( !status ); /* create a thread at a lower priority */ status = pthread_create( &Task2_id, NULL, Task_2, NULL ); rtems_test_assert( !status ); /* set priority of Task2 to highest priority */ param.sched_priority = sched_get_priority_max( SCHED_FIFO ); puts( "Init: pthread_setschedparam - Setting Task2 priority to highest" ); status = pthread_setschedparam( Task2_id, SCHED_FIFO, ¶m ); rtems_test_assert( !status ); /* switching to Task2 */ status = pthread_getschedparam( pthread_self(), &policy, ¶m ); rtems_test_assert( !status ); printf( "Init: pthread_getschedparam - priority = %d\n", param.sched_priority); puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); status = pthread_mutex_unlock( &Mutex2_id ); rtems_test_assert( !status ); puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" ); status = pthread_mutexattr_destroy( &attr ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_destroy - SUCCESSFUL" ); status = pthread_mutex_destroy( &Mutex2_id ); rtems_test_assert( !status ); /* priority ceiling mutex */ empty_line(); puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); status = pthread_mutexattr_init( &attr ); rtems_test_assert( !status ); puts( "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_PROTECT)" ); status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_init - SUCCESSFUL" ); status = pthread_mutex_init( &Mutex2_id, &attr ); rtems_test_assert( !status ); puts( "Init: pthread_mutex_getprioceiling - EINVAL (invalid id)" ); status = pthread_mutex_getprioceiling( &Mutex_bad_id, &ceiling ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutex_getprioceiling - EINVAL (NULL ceiling)" ); status = pthread_mutex_getprioceiling( &Mutex2_id, NULL ); rtems_test_assert( status == EINVAL ); status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling ); rtems_test_assert( !status ); printf( "Init: pthread_mutex_getprioceiling - %d\n", ceiling ); puts( "Init: pthread_mutex_setprioceiling - EINVAL (invalid id)" ); status = pthread_mutex_setprioceiling( &Mutex_bad_id, 200, &old_ceiling ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutex_setprioceiling - EINVAL (illegal priority)" ); status = pthread_mutex_setprioceiling( &Mutex2_id, 512, &old_ceiling ); rtems_test_assert( status == EINVAL ); puts( "Init: pthread_mutex_setprioceiling - EINVAL (NULL ceiling)" ); status = pthread_mutex_setprioceiling( &Mutex2_id, 128, NULL ); rtems_test_assert( status == EINVAL ); /* normal cases of set priority ceiling */ priority = sched_get_priority_max( SCHED_FIFO ); priority = (priority == 254) ? 200 : 13; printf( "Init: pthread_mutex_setprioceiling - new ceiling = %d\n", priority ); status = pthread_mutex_setprioceiling( &Mutex2_id, priority, &old_ceiling ); rtems_test_assert( !status ); printf( "Init: pthread_mutex_setprioceiling - old ceiling = %d\n",old_ceiling ); status = pthread_getschedparam( pthread_self(), &policy, ¶m ); rtems_test_assert( !status ); printf( "Init: pthread_getschedparam - priority = %d\n", param.sched_priority ); puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); status = pthread_mutex_trylock( &Mutex2_id ); rtems_test_assert( !status ); status = pthread_getschedparam( pthread_self(), &policy, ¶m ); rtems_test_assert( !status ); printf( "Init: pthread_getschedparam - priority = %d\n", param.sched_priority ); /* create a thread at a higher priority */ status = pthread_create( &Task3_id, NULL, Task_3, NULL ); rtems_test_assert( !status ); /* set priority of Task3 to highest priority */ param.sched_priority = --priority; status = pthread_setschedparam( Task3_id, SCHED_FIFO, ¶m ); rtems_test_assert( !status ); puts( "Init: pthread_setschedparam - set Task3 priority to highest" ); /* DOES NOT SWITCH to Task3 */ puts( "Init: Sleep 1 second" ); rtems_test_assert( !status ); sleep( 1 ); /* switch to task 3 */ puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); status = pthread_mutex_unlock( &Mutex2_id ); rtems_test_assert( !status ); status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling ); rtems_test_assert( !status ); printf( "Init: pthread_mutex_getprioceiling- ceiling = %d\n", ceiling ); /* set priority of Init to highest priority */ param.sched_priority = sched_get_priority_max(SCHED_FIFO); status = pthread_setschedparam( Init_id, SCHED_FIFO, ¶m ); rtems_test_assert( !status ); puts( "Init: pthread_setschedparam - set Init priority to highest" ); puts( "Init: pthread_mutex_lock - EINVAL (priority ceiling violation)" ); status = pthread_mutex_lock( &Mutex2_id ); if ( status != EINVAL ) printf( "status = %d\n", status ); rtems_test_assert( status == EINVAL ); /* mutexinit.c: Initialising recursive mutex */ puts( "Init: Recursive Mutex" ); status = pthread_mutex_destroy( &Mutex2_id ); if( status ) printf( "status mutex destroy:%d\n", status ); status = pthread_mutexattr_init( &attr ); if( status ) printf( "status mutexattr:%d\n", status ); attr.recursive=true; status = pthread_mutex_init( &Mutex2_id, &attr ); if ( status ) printf( "status recursive mutex :%d\n", status ); rtems_test_assert( !status ); TEST_END(); rtems_test_exit( 0 ); return NULL; /* just so the compiler thinks we returned something */ }