how to use mutex

Chan Kim ckim at etri.re.kr
Wed Sep 2 02:28:16 UTC 2015


I tried it with semaphore
for init,
    rtems_attribute sem_attr;
    rtems_status_code status;
    sem_attr = RTEMS_INHERIT_PRIORITY | RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY;
    sem_name = rtems_build_name( 'S','0',' ',' ');
    status = rtems_semaphore_create( sem_name, 1, sem_attr, 0, &sem_id);
    printk("semaphore created status = %d\n", status);

to obtain, (read case, write thread the same)
    rtems_status_code status;
    status = rtems_semaphore_obtain( sem_id, RTEMS_WAIT, 0);
    printk("locked from read..status = %d\n", status);

to release (read case, write thread the same)
    printk("unlocking from read..\n");
    rtems_semaphore_release( sem_id);

I get result
...
locked from read..status = 0
unlocking from read..
locked from read..status = 0
unlocking from read..
locked from read..status = 0
unlocking from read..
locked from read..status = 0
unlocking from read..
locked from read..status = 0
unlocking from read..
locked from read..status = 0
unlocking from read..
locked from read..status = 0
unlocking from read..
locked from read..status = 0
unlocking from read..
locked from read..status = 4
unlocking from read..
locked from read..status = 4
unlocking from read..
locked from read..status = 4
unlocking from read..
locked from write.. status = 4
unlocking from write..
locked from write.. status = 4
unlocking from write..
locked from write.. status = 4
unlocking from write..
locked from read..status = 4
locked from write.. status = 4   <===== read locked it, but write entered it.

status code 4 is RTEMS_INVALID_ID meaning object ID is invalid. I don't know what's wrong.
can anyone tell me what's wrong?
Chan



보낸 사람 : "Chan Kim" <ckim at etri.re.kr>
보낸 날짜 : 2015-09-02 10:26:24 ( +09:00 )
받는 사람 : users at rtems.org <users at rtems.org>
참조 : 
제목 : how to use mutex

Hello, rtems users,

Please somebody help with with using mutex.
To avoid sync problem by simpling serializing read and write processing, I tried using mutex and found it's not working properly.

------------ the way I did it 
pthread_mutexattr_t mutex_attr;
pthread_mutex_t rd_wr_mutex;

to initialize,
statusi = pthread_mutexattr_init( &mutex_attr );
printk("mutext attri init result = %x\n", statusi);
pthread_mutex_init(&rd_wr_mutex, &mutex_attr);
printk("mutext init result = %x\n", statusi);

to lock,
pthread_mutex_lock(&rd_wr_mutex); // rtems_mutex_t
printk("locked from read..\n");

to unlock,
printk("unlocking from read..\n");
pthread_mutex_unlock(&rd_wr_mutex);

--------------
I see results (only mutex lock, unlock prints)

mutext attr init result = 0
mutext init result = 0
...
locked from read..
unlocking from read..
locked from write..
unlocking from write..
locked from write..
unlocking from write..
locked from write..
unlocking from write..
locked from read..
locked from write.. <===== ???


Print result shows teh write thread enters it when read thread has locked it. What's wrong with the way of my using mutex?
Hope someone clears it with some explanation.
Thanks,

regards,
Chan Kim
_______________________________________________
users mailing list
users at rtems.org
http://lists.rtems.org/mailman/listinfo/users


More information about the users mailing list