pthread_rwlock_timedwrlock_np()--Get Exclusive Write Lock with Time-Out


  Syntax:
 #include <pthread.h>
 #include <time.h>
 int pthread_rwlock_timedwrlock_np(pthread_rwlock_t *rwlock,   
 const struct timespec *deltatime);
  Service Program Name: QP0WPTHR

  Default Public Authority: *USE

  Threadsafe: Yes

  Signal Safe: Yes

The pthread_rwlock_timedwrlock_np() function attempts to acquire an exclusive write lock on the read/write lock specified by rwlock. If the exclusive write lock cannot be acquired in the deltatime specific, pthread_rwlock_timedwrlock_np() returns the EBUSY error.

Only one thread can hold an exclusive write lock on a read/write lock object. If any thread holds an exclusive write lock on a read/write lock object, no other threads are allowed to hold a shared read or exclusive write lock.

If no threads are holding an exclusive write lock or shared read lock on the read/write lock, the calling thread successfully acquires the exclusive write lock.

If the calling thread already holds an exclusive write lock on the read/write lock, another write lock can be successfully acquired by the calling thread. If more than one exclusive write lock is successfully acquired by a thread on a read/write lock object, that thread is required to successfully call pthread_rwlock_unlock() a matching number of times.

With a large number of readers and relatively few writers, there is the possibility of writer starvation. If threads are waiting for an exclusive write lock on the read/write lock and there are threads that currently hold a shared read lock, the subsequent attempts to acquire a shared read lock request are granted, while attempts to acquire the exclusive write lock wait.

If the read/write lock is destroyed while pthread_rwlock_timedwrlock_np() is waiting for the shared read lock, the EDESTROYED error is returned.

If a signal is delivered to the thread while it is waiting for the lock, the signal handler (if any) runs, and the thread resumes waiting. For a timed wait, when the thread resumes waiting after the signal handler runs, the wait time is reset. For example, suppose a thread specifies that it should wait for a lock for 5 seconds, and a signal handler runs in that thread after 2.5 seconds. After returning from the signal handler, the thread resumes its wait for another 5 seconds. The resulting wait is longer than the specified 5 seconds.


Read/Write Lock Deadlocks

If a thread ends while holding a write lock, the attempt by another thread to acquire a shared read or exclusive write lock will not succeed. In this case, the attempt to acquire the lock returns the EBUSY error after the specified time elapses for the lock operation. If a thread ends while holding a read lock, the system automatically releases the read lock.

For the pthread_rwlock_timedwrlock_np() function, the pthreads run-time simulates the deadlock that has occurred in your application. When you are attempting to debug these deadlock scenarios, the CL command WRKJOB, option 20, shows the thread as in a condition wait. Displaying the call stack shows that the function timedDeadlockOnOrphanedRWLock is in the call stack.


Upgrade / Downgrade a Lock

If the calling thread currently holds a shared read lock on the read/write lock object and no other threads are holding a shared read lock, the exclusive write request is granted. After the exclusive write lock request is granted, the calling thread holds both the shared read and the exclusive write lock for the specified read/write lock object. If the thread calls pthread_rwlock_unlock() while holding one or more shared read locks and one or more exclusive write locks, the exclusive write locks are unlocked first. If more than one outstanding exclusive write lock was held by the thread, a matching number of successful calls to pthread_rwlock_unlock() must be done before all write locks are unlocked. At that time, subsequent calls to pthread_rwlock_unlock() unlock the shared read locks.

You can use this behavior to allow your application to upgrade or downgrade one lock type to another. See Read/write locks can be upgraded and downgraded.


Authorities and Locks

None.


Parameters

rwlock
(Input) The address of the read/write lock
deltatime
(Input) The number of seconds and nanoseconds to wait for the lock before returning an error

Return Value

0
pthread_rwlock_timedwrlock_np() was successful.
value
pthread_rwlock_timedwrlock_np() was not successful. value is set to indicate the error condition.

Error Conditions

If pthread_rwlock_timedwrlock_np() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.

[EINVAL]

The value specified for the argument is not correct.

[EBUSY]

The lock could not be acquired in the time specified.

[EDESTROYED]

The lock was destroyed while waiting.


Related Information


Example

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include "check.h"

pthread_rwlock_t       rwlock = PTHREAD_RWLOCK_INITIALIZER;

void *wrlockThread(void *arg)
{
  int             rc;
  int             count=0;
  struct timespec ts;

  /* 1.5 seconds */
  ts.tv_sec = 1;
  ts.tv_nsec = 500000000;

  printf("%.8x %.8x: Entered thread, getting write lock with timeout\n",
         pthread_getthreadid_np());
  Retry:
  rc = pthread_rwlock_timedwrlock_np(&rwlock, &ts);
  if (rc == EBUSY) {
    if (count >= 10) {
      printf("%.8x %.8x: Retried too many times, failure!\n",
             pthread_getthreadid_np());
      exit(EXIT_FAILURE);
    }
    ++count;
    printf("%.8x %.8x: RETRY...\n", pthread_getthreadid_np());
    goto Retry;
  }
  checkResults("pthread_rwlock_wrlock() 1\n", rc);
  printf("%.8x %.8x: Got the write lock\n", pthread_getthreadid_np());

  sleep(2);

  printf("%.8x %.8x: Unlock the write lock\n",
         pthread_getthreadid_np());
  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()\n", rc);

  printf("%.8x %.8x: Secondary thread complete\n", pthread_getthreadid_np());
  return NULL;
}

int main(int argc, char **argv)
{
  int                   rc=0;
  pthread_t             thread, thread2;

  printf("Enter Testcase - %s\n", argv[0]);

  printf("Main, get the write lock\n");
  rc = pthread_rwlock_wrlock(&rwlock);
  checkResults("pthread_rwlock_wrlock()\n", rc);

  printf("Main, create the timed write lock threads\n");
  rc = pthread_create(&thread, NULL, wrlockThread, NULL);
  checkResults("pthread_create\n", rc);

  rc = pthread_create(&thread2, NULL, wrlockThread, NULL);
  checkResults("pthread_create\n", rc);

  printf("Main, wait a bit holding this write lock\n");
  sleep(3);

  printf("Main, Now unlock the write lock\n");
  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()\n", rc);

  printf("Main, wait for the threads to end\n");
  rc = pthread_join(thread, NULL);
  checkResults("pthread_join\n", rc);

  rc = pthread_join(thread2, NULL);
  checkResults("pthread_join\n", rc);

  rc = pthread_rwlock_destroy(&rwlock);
  checkResults("pthread_rwlock_destroy()\n", rc);
  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPRWLWR0
Main, get the write lock
Main, create the timed write lock threads
Main, wait a bit holding this write lock
00000000 00000017: Entered thread, getting write lock with timeout
00000000 00000018: Entered thread, getting write lock with timeout
00000000 00000017: RETRY...
00000000 00000018: RETRY...
Main, Now unlock the write lock
Main, wait for the threads to end
00000000 00000017: Got the write lock
00000000 00000018: RETRY...
00000000 00000018: RETRY...
00000000 00000017: Unlock the write lock
00000000 00000017: Secondary thread complete
00000000 00000018: Got the write lock
00000000 00000018: Unlock the write lock
00000000 00000018: Secondary thread complete
Main completed

API introduced: V4R3

[ Back to top | Pthread APIs | APIs by category ]