Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsafe locking of critical sections #3

Open
GoogleCodeExporter opened this issue Mar 27, 2015 · 0 comments
Open

Unsafe locking of critical sections #3

GoogleCodeExporter opened this issue Mar 27, 2015 · 0 comments

Comments

@GoogleCodeExporter
Copy link

the following code sequence is not safe:

   while (context->event_stack_lock) sceKernelDelayThread(1000);
   context->event_stack_lock = 1;
   ...do something...
   context->event_stack_lock = 0;

A thread reschedule could happen between the test in the "while" and setting 
the lock to "1". The 2 threads could both think the lock is free and both enter 
the critical section at the same time.
Better would be to use a Mutex:
   context->event_stack_mutex = sceKernelCreateMutex("Event Stack", PSP_MUTEX_ATTR_ALLOW_RECURSIVE, 0, NULL);

   sceKernelLockMutex(context->event_stack_mutex, 1, NULL);
   ...do something...
   sceKernelUnlockMutex(context->event_stack_mutex, 1);

The attribute PSP_MUTEX_ATTR_ALLOW_RECURSIVE allows a thread to lock again a 
mutex when it has already been locked by himself. This could be useful to also 
protect the sequence below, see Issue 2 (_spawnLocalEvent will again lock the 
mutex):

   sceKernelLockMutex(context->event_stack_mutex, 1, NULL);
   _spawnLocalEvent(context, ADHOC_MATCHING_EVENT_ESTABLISHED, sendermac, 0, NULL);
   _spawnLocalEvent(context, ADHOC_MATCHING_EVENT_ACCEPT, sendermac, optlen, opt);
   sceKernelUnlockMutex(context->event_stack_mutex, 1);

Original issue reported on code.google.com by gi...@web.de on 12 Jul 2012 at 7:20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant