developping kernel in C11

Programming, for all ages and all languages.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: developping kernel in C11

Post 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.)
Developer of tyndur - community OS of Lowlevel (German)
h0bby1
Member
Member
Posts: 240
Joined: Wed Aug 21, 2013 7:08 am

Re: developping kernel in C11

Post 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

Code: Select all

(*current_lock)=this_thread;
just after

Code: Select all

c_lock=(*current_lock);
?


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
Post Reply