LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-30-2010, 01:04 AM   #1
sanjeebkdeka
LQ Newbie
 
Registered: Feb 2010
Posts: 8

Rep: Reputation: 0
Releasing resources in pthread_cond_wait()/pthread_cond_timedwait()


We know that cancellation signals could be noticed during pthread_cond_wait()/pthread_cond_timedwait(). Now, at this point if a thread gets canceled we could release the resources held by the thread by using cleanup handlers:

Eg:
pthread_cleanup_push(pthread_mutex_unlock, mutex);
pthread_cond_wait(cv, mutex);
pthread_cleanup_pop(0);

Supppose, my codebase has 5 pthread_cond_wait() and each of the call to the pthread_cond_wait() is accompanied by cleanup handlers as shown in the example above.

Now ,let us consider that the thread has got canceled during the call to the 5th pthread_cond_wait(), i.e the thread will have 4 locks held at that point in time.
My question here is as the thread gets canceled during the call to the 5th pthread_cond_wait(), whether all the cleanup handlers will be called at this point in time or only that cleanup handler, registered for the 5th pthread_cond_wait(), will be called.
 
Old 03-30-2010, 03:51 PM   #2
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Hey,

from the man page of pthread_cleanup_push(3), I would say: YES, all the clean-up handlers should be called IFF the calls to pthread_cleanup_push(3) are nested as below

man 3 pthread_cleanup_push:
Quote:
"These functions manipulate the calling thread's stack of thread-cancellation clean-up handlers. A clean-up handler is a function that is automatically executed when a thread is canceled [...] it might, for example, unlock a mutex so that it becomes available to other threads in the process.

The pthread_cleanup_push() function pushes routine onto the top of the stack of clean-up handlers."
Though, the clean-up stacks are per-thread and not per process, meaning each thread has a separate clean-up stack!

man 3 pthread_cancel:
Quote:
"When a cancellation requested is acted on, the following steps occur for thread (in this order):

1. Cancellation clean-up handlers are popped (in the reverse of the order in which they were pushed) and called.
By saying nested, I meant a schema like the following (pseudo-code):

Code:
push(func1);
/* actions 1 */
push(func2);
/* actions 2 */
pop(0);
/* actions 1 */
pop(0);
Is that what you have? A pthread_cond_wait(3)/pthread_cond_signal(3) mechanism with 5 nesting levels???

- Andi -
 
Old 04-01-2010, 02:24 AM   #3
sanjeebkdeka
LQ Newbie
 
Registered: Feb 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Releasing resources in pthread_cond_wait()/pthread_cond_timedwait()

Hi Andi,

Thanks for you reply.

In my case, all the cleanup handlers registered through pthread_cleanup_push() may not be in the same lexical context.
I might be registering cleanup handlers involving mutex variable
which does not have global scope.

Do you mean all the registered handlers will be called in this case also?

Regards,
San

Quote:
Originally Posted by ForzaItalia2006 View Post
Hey,

from the man page of pthread_cleanup_push(3), I would say: YES, all the clean-up handlers should be called IFF the calls to pthread_cleanup_push(3) are nested as below

man 3 pthread_cleanup_push:


Though, the clean-up stacks are per-thread and not per process, meaning each thread has a separate clean-up stack!

man 3 pthread_cancel:


By saying nested, I meant a schema like the following (pseudo-code):

Code:
push(func1);
/* actions 1 */
push(func2);
/* actions 2 */
pop(0);
/* actions 1 */
pop(0);
Is that what you have? A pthread_cond_wait(3)/pthread_cond_signal(3) mechanism with 5 nesting levels???

- Andi -
 
Old 04-02-2010, 04:55 AM   #4
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Hey San,

Quote:
Originally Posted by sanjeebkdeka View Post
In my case, all the cleanup handlers registered through pthread_cleanup_push() may not be in the same lexical context.
I might be registering cleanup handlers involving mutex variable
which does not have global scope.

Do you mean all the registered handlers will be called in this case also?

Regards,
San
I think I haven't yet completely understood your scenario. The general theory according to the man pages is that all the registered cleanup-handlers will be called in reversed registration order when the thread gets cancelled.

Regarding the lexical context, I'm not sure if that works. Linux implements the pthread_cleanup_push(3) and pthread_cleanup_pop(3) by the use of macros so that those two functions should/must be in the same context, because pthread_cleanup_push(3) starts an opening brace while pthread_cleanup_pop(3) finishes the section by a closing brace.

Although possibly not possible in the pthread context, you shouldn't in general register a (global) cleanup-handler for a non-global mutex object, because the object might already be destroyed when the cleanup handler gets called.

If my answer isn't really clear, you could possibly post some parts of your code ...

- Andi -
 
Old 04-07-2010, 10:24 PM   #5
sanjeebkdeka
LQ Newbie
 
Registered: Feb 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Hi Andi,

Thanks for your reply. Actually, i am an infant in the field of multi-threading. However, i have understood what i need to do from your replies.

One more thing...my code snippet is somewhat as follows:

pthread_mutex_lock(lock);
-----------------------
There may be some statements here
------------------------
Some api that is a thread cancellation point.
[pthread_cond_wait()/pthread_cond_timedwait()/close() etc.]
--------------------
There may be some statements here
--------------------
pthread_mutex_unlock(lock);


In this case, my understanding is the thread is holding a lock while executing the api that is cancellation point. Now if the thread somehow gets canceled (due to some signal etc.) at this point in time, the lock will never be released and other threads contending for that lock would hang.

So, i want to modify my code as follows:

pthread_cleanup_push(pthread_mutex_unlock, lock);

pthread_mutex_lock(lock);
------------------------------
There may be some statements here
--------------------------------

Some api that is a thread cancellation point.
[pthread_cond_wait()/pthread_cond_timedwait()/close() etc.]
---------------------------
There may be some statements here
---------------------------------

pthread_cleanup_pop(0);

pthread_mutex_unlock(lock);

I want to know whether this is a correct fix.

Regards,
San

Quote:
Originally Posted by ForzaItalia2006 View Post
Hey San,



I think I haven't yet completely understood your scenario. The general theory according to the man pages is that all the registered cleanup-handlers will be called in reversed registration order when the thread gets cancelled.

Regarding the lexical context, I'm not sure if that works. Linux implements the pthread_cleanup_push(3) and pthread_cleanup_pop(3) by the use of macros so that those two functions should/must be in the same context, because pthread_cleanup_push(3) starts an opening brace while pthread_cleanup_pop(3) finishes the section by a closing brace.

Although possibly not possible in the pthread context, you shouldn't in general register a (global) cleanup-handler for a non-global mutex object, because the object might already be destroyed when the cleanup handler gets called.

If my answer isn't really clear, you could possibly post some parts of your code ...

- Andi -

Last edited by sanjeebkdeka; 04-07-2010 at 10:27 PM. Reason: .
 
Old 04-08-2010, 12:46 PM   #6
sanjeebkdeka
LQ Newbie
 
Registered: Feb 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Hi Andi,

Thanks for your reply. Actually, i am an infant in the field of multi-threading. However, i have understood what i need to do from your replies.

One more thing...my code snippet is somewhat as follows:

pthread_mutex_lock(lock);
-----------------------
There may be some statements here
------------------------
Some api that is a thread cancellation point.
[pthread_cond_wait()/pthread_cond_timedwait()/close() etc.]
--------------------
There may be some statements here
--------------------
pthread_mutex_unlock(lock);


In this case, my understanding is the thread is holding a lock while executing the api that is cancellation point. Now if the thread somehow gets canceled (due to some signal etc.) at this point in time, the lock will never be released and other threads contending for that lock would hang.

So, i want to modify my code as follows:

pthread_cleanup_push(pthread_mutex_unlock, lock);

pthread_mutex_lock(lock);
------------------------------
There may be some statements here
--------------------------------

Some api that is a thread cancellation point.
[pthread_cond_wait()/pthread_cond_timedwait()/close() etc.]
---------------------------
There may be some statements here
---------------------------------

pthread_cleanup_pop(0);

pthread_mutex_unlock(lock);

I want to know whether this is a correct fix.

Regards,
San
 
Old 04-12-2010, 01:05 PM   #7
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Hey,

sorry for the late response :-)

Quote:
Originally Posted by sanjeebkdeka View Post
In this case, my understanding is the thread is holding a lock while executing the api that is cancellation point. Now if the thread somehow gets canceled (due to some signal etc.) at this point in time, the lock will never be released and other threads contending for that lock would hang.
You shouldn't mix up signal delivery (to threads) and thread cancellation. While signal delivery is implemented by the kill(2) or pthread_kill(3) API-calls, thread cancellation is done by explicitly calling pthread_cancel(3). The thread's behavior of both is also configured differently. Thread cancellation only occurs if the thread(s) enable cancellation and a different thread calls pthread_cancel(3), though it's part of your design and not influenced by external signals.


Quote:
Originally Posted by sanjeebkdeka View Post
So, i want to modify my code as follows:

pthread_cleanup_push(pthread_mutex_unlock, lock);

pthread_mutex_lock(lock);
------------------------------
There may be some statements here
--------------------------------

Some api that is a thread cancellation point.
[pthread_cond_wait()/pthread_cond_timedwait()/close() etc.]
---------------------------
There may be some statements here
---------------------------------

pthread_cleanup_pop(0);

pthread_mutex_unlock(lock);

I want to know whether this is a correct fix.
From a first view, I would say this looks good. When implementing the pthread_cleanup_push/pop(3) functions, you should basically check which API functions (acting upon cancellation requests) are called within your section to get a good overview what needs to be done in the clean-up handlers to really clean-up all used resources. To get a list of cancellation points in API functions, check the man page pthreads(7) and look for 'Cancellation Points'. pthread_cond_wait(3) for example is a cancellation point. By then further looking into the man page of the function, you'll get information about what this function is doing when a thread has a pending cancellation.

I hope that helps to address any of these problems in the future :-)

Andi
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Why does pthread_cond_wait need a mutex? fuzzyBuzz Programming 4 06-01-2009 02:16 PM
CPU Usage goes to 100% when pthread_cond_wait is being used zen_buddha Linux - Software 4 10-19-2005 04:28 PM
CPU Usage goes to 100% when pthread_cond_wait is being used zen_buddha Programming 0 10-13-2005 03:48 AM
POSIX threads - pthread_cond_timedwait iklinux Programming 5 09-26-2005 10:33 AM
Should I use sleep or pthread_cond_wait? George2 Programming 8 08-10-2005 09:10 AM

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

All times are GMT -5. The time now is 09:18 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