Page 2 of 2
Re: developping kernel in C11
Posted: Mon Mar 10, 2014 4:32 am
by Kevin
h0bby1 wrote:Code: Select all
int *current_lock=&lock.current_lock;
if((*current_lock)!=0)
{
add_this_thread_to_queue(lock);
wait();
}
(*current_lock)=this_thread
logically there is nothing that would prevent the compiler to put the (*current_lock)=this_thread before the wait
Except that it probably doesn't know whether add_this_thread_to_queue() or wait() can access current_lock or *current_lock, so in practice it can't reorder. (And if it knew the answer, it would probably be "yes", so it wouldn't be able to reorder either.)
Re: developping kernel in C11
Posted: Mon Mar 10, 2014 5:43 am
by h0bby1
Kevin wrote:h0bby1 wrote:Code: Select all
int *current_lock=&lock.current_lock;
if((*current_lock)!=0)
{
add_this_thread_to_queue(lock);
wait();
}
(*current_lock)=this_thread
logically there is nothing that would prevent the compiler to put the (*current_lock)=this_thread before the wait
Except that it probably doesn't know whether add_this_thread_to_queue() or wait() can access current_lock or *current_lock, so in practice it can't reorder. (And if it knew the answer, it would probably be "yes", so it wouldn't be able to reorder either.)
Code: Select all
struct lock
{
unsigned int current_lock;
array queue;
};
void add_this_thread_to_queue(array queue)
{
queue.add(this_thread);
}
void wait()
{
}
void lock(struct *lock)
{
int *current_lock=&lock->current_lock;
int c_lock;
c_lock=(*current_lock);
if(c_lock!=0)
{
add_this_thread_to_queue(&lock->queue);
wait();
}
(*current_lock)=this_thread;
in that case there is nothing that would prevent the compiler to reorder
just after
?
but it's just an example, i might need to control how the compiler order things in other parts if i want to do a lockless access to list, where the way the compiler can be ordering access can be more confusing