解锁后pthread_mutex值发生变化

时间:2015-02-14 20:12:05

标签: c++ c multithreading pthreads mutex

我在Android(L)BT应用程序上运行SnS并在bluedroid下使用GKI调用。所有GKI调用都是从BT应用程序的各个线程调用的,我们通过互斥(仿生)保护它。如果BT打开\关闭700+迭代它工作正常但在此之后突然我看到互斥锁被一个线程解锁的那一刻,它的值变为2并且它没有回到它的旧值。我以默认方式初始化互斥锁,并且确实不确定发生了什么。事实上,我已经在缓冲区周围放置了缓冲区填充以避免任何损坏,但是模式缓冲区根本没有被覆盖,只有互斥锁值变为2。所有等待这个互斥体的线程都会挨饿。

请说明为什么它可以将其值更改为2以及如何阻止它。

  Line 3121: 01-01 03:30:30.200  1117 15470 E GKI_LINUX: LOCK TRYING : gki addr 0xa4e18e40 **value 16384** value before=1515870810 and after=1515870810
                Line 3123: 01-01 03:30:30.200  1117 15470 E GKI_LINUX: LOCKED : gki addr 0xa4e18e40 value 1013858305 value before=1515870810 and after=1515870810

  Line 3139: 01-01 03:30:30.200  1117 15470 E GKI_LINUX: UNLOCKED : gki addr 0xa4e18e40 **value 2** value before=1515870810 and after=1515870810  **//ideally it should have been 16384**

P.S。 :新手到linux

代码:

//This function initializes the mutex.
void GKI_init(void)
{
    pthread_mutexattr_t attr;
    tGKI_OS             *p_os;

    memset (&gki_cb, 0, sizeof (gki_cb));

    gki_buffer_init();
    gki_timers_init();
    alarm_service_init();

    gki_cb.com.OSTicks = (UINT32) times(0);

    pthread_mutexattr_init(&attr);

#ifndef __CYGWIN__
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
#endif
    p_os = &gki_cb.os;
    pthread_mutex_init(&p_os->GKI_mutex, &attr);
    /* pthread_mutex_init(&GKI_sched_mutex, NULL); */
    /*Store mutex value in backup */
    p_os->origval = p_os->GKI_mutex.value;
    ALOGE(" backup mutex value is = %d ",p_os->origval);

#if (GKI_DEBUG == TRUE)
    pthread_mutex_init(&p_os->GKI_trace_mutex, NULL);
#endif 

    struct sigevent sigevent;
    memset(&sigevent, 0, sizeof(sigevent));
    sigevent.sigev_notify = SIGEV_THREAD;
    sigevent.sigev_notify_function = (void (*)(union sigval))bt_alarm_cb;
    sigevent.sigev_value.sival_ptr = NULL;
    if (timer_create(CLOCK_REALTIME, &sigevent, &posix_timer) == -1) {
        ALOGE("%s unable to create POSIX timer: %s", __func__, strerror(errno));
        timer_created = false;
    } else {
        timer_created = true;
    }
}


void GKI_disable (void)
{
  pthread_mutex_lock(&gki_cb.os.GKI_mutex);
}

void GKI_enable (void)
{
    pthread_mutex_unlock(&gki_cb.os.GKI_mutex);
//Just after this mutex value is getting changed i.e. GKI_mutex.value =2
}

每次在访问公共资源之前调用GKI_disable / GKI_enable。

0 个答案:

没有答案