LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2009, 04:22 AM   #1
haikan
LQ Newbie
 
Registered: Jul 2009
Location: Ålesund, Norway
Distribution: Slackware, Linux Mint
Posts: 21

Rep: Reputation: 3
CLOCK_MONOTONIC and sem_timedwait / pthread_mutex_timedlock


Hi!

The use of CLOCK_MONOTONIC is somewhat limited as it's not supported AFAIK for semaphores and mutexes. I know it is supported for condition variables through pthread_condattr_setclock().

The only reference I found on this subject is a question from Ulrich Drepper to the OpenGroup about the lack of:
pthread_mutexattr_getclock()
pthread_mutexattr_setclock()
pthread_rwlockattr_getclock()
pthread_rwlockattr_setclock()
sem_getclock()
sem_setclock()
posix_trace_attr_setclock()
posix_trace_attr_getclock()

Link to original message is here:
http://www.opengroup.org/austin/mail.../msg03679.html

Does anyone know if there's any progress on CLOCK_MONOTONIC support for mutexes and semaphores?

Oscar Endre Edvardsen
 
Old 08-08-2009, 08:03 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

The beauty part about Open Source is that, if one toolkit happens to have one or another limitation, you can usually either:

a) modify the available source to do what you want

... or (more commonly) ...

b) find an alternate toolkit that does exactly what you need/want.

Personally, I think the standard pthreads library and "gettimeofday()" probably already does everything you actually need. But there are *lots* of alternatives out there.
 
Old 08-09-2009, 05:05 PM   #3
haikan
LQ Newbie
 
Registered: Jul 2009
Location: Ålesund, Norway
Distribution: Slackware, Linux Mint
Posts: 21

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by paulsm4 View Post
Hi -
Personally, I think the standard pthreads library and "gettimeofday()" probably already does everything you actually need. But there are *lots* of alternatives out there.
Thanks for your reply!

gettimeofday uses the realtime clock, not CLOCK_MONOTONIC. This is how pthread_cond_timedwait is set up to use CLOCK_MONOTONIC:
Code:
pthread_condattr_t condattr;
assert( pthread_condattr_init(&condattr) == 0);
assert( pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC) == 0);

pthread_cond_t cond;
assert( pthread_cond_init(&cond, &condattr) == 0);
Semaphores and mutexes also has timedwait functions (sem_timedwait()/pthread_mutex_timedlock()) but these can not as of SUSv3 be configured to use clockids other then CLOCK_REALTIME.

--
Oscar Endre Edvardsen
 
Old 08-19-2013, 07:59 PM   #4
blip
LQ Newbie
 
Registered: Aug 2013
Posts: 17

Rep: Reputation: Disabled
It is possible to work around the lack of CLOCK_MONOTONIC in some cases but it requires a POSIX timer and an unused signal number. The timer can use CLOCK_REALTIME since it will be relative only, so I *think* that system clock adjustments shouldn't affect it.

Set up a stub signal handler with sigaction() and unmask the signal using pthread_sigmask(). Then use timer_create() to create a timer which will signal the thread on expiration and call timer_settime() to start it counting down. Unlike sem_wait(), the pthread_*() functions appear never to allow a signal handler interrupt them. So, if you want to use pthread_mutex_timedlock(), emulate the mutex (unlocked state) with a semaphore initialized to 1. Now when the signal fires and returns, sem_wait() will return a non-zero value and errno will be set to EINTR. To guard against external signal sources and to ensure the timer actually expired, use a loop to check it with timer_gettime().

There is a per-process limit of POSIX timers. If I remember correctly, it was 256 on FreeBSD and 1024 on Linux last I checked. If you need that many, consider timer multiplexing or rethink your design.

EDIT:
Sorry, nevermind. There is a race condition.

Last edited by blip; 08-20-2013 at 08:25 PM.
 
1 members found this post helpful.
Old 04-22-2014, 07:50 PM   #5
blip
LQ Newbie
 
Registered: Aug 2013
Posts: 17

Rep: Reputation: Disabled
I apologize for my previous misleading post and for double posting. However, it really is possible to implement my_sem_timedwait() and my_mutex_timedlock() using a monotonic clock, with restrictions.

Implement each semaphore with a mutex, condition variable, and counter. Call pthread_condattr_setclock() when initializing your emulated semaphore. There are drawbacks related to signals: my_sem_post() isn't safe to call within a signal handler, nor is my_sem_wait()/my_sem_timedwait() interruptable by signals. Finally, use an emulated semaphore to implement my_mutex_timedlock() with a monotonic clock.

These emulated semaphores are also useful on Mac OS X where unnamed semaphores just aren't implemented.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
clock_gettime(CLOCK_MONOTONIC,..) fails nathan2225 Programming 7 07-21-2006 09:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration