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.)h0bby1 wrote:logically there is nothing that would prevent the compiler to put the (*current_lock)=this_thread before the waitCode: Select all
int *current_lock=&lock.current_lock; if((*current_lock)!=0) { add_this_thread_to_queue(lock); wait(); } (*current_lock)=this_thread
developping kernel in C11
Re: developping kernel in C11
Re: developping kernel in C11
Kevin wrote: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.)h0bby1 wrote:logically there is nothing that would prevent the compiler to put the (*current_lock)=this_thread before the waitCode: Select all
int *current_lock=&lock.current_lock; if((*current_lock)!=0) { add_this_thread_to_queue(lock); wait(); } (*current_lock)=this_thread
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;
Code: Select all
(*current_lock)=this_thread;
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