[Date Prev][Date Next]
[LMDB] inconsistent mutex behavior and problems for M:N threading
- To: firstname.lastname@example.org
- Subject: [LMDB] inconsistent mutex behavior and problems for M:N threading
- From: David Barbour <email@example.com>
- Date: Fri, 5 Dec 2014 18:29:57 -0600
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=iNaH71D+WwOm7y4FE5ejweMcYQ4XP5NawHmbva8iOuE=; b=KBvyD1Jymntm0aBHOWeBpBT/WyJ/v+Z9v/R6uNHQjGd7RmxCaorDonYFS04RnPaAec EtPognX3lYi9sTxQTyPeR0i4xtJXIy/xmI5WS4q5mdTWXy+0Y7XuODUbVbZzVomvBKNz LBbeXU140j9mMaNd8w1UALEtEFvQB6PIPIezE4Rv8eBVHGqXBpgpn1Oc1PuOndSpgDQE S+aEFoX6mHRgpuXfdT4Y6WjY7FMHMFJpPEimU675MRiwTjgSTjmkBG9P7mBbSwKo7iZo DUTHPx2x8K2sLLdEEZ5dxv5OnX68WEimDD9LuzBFlm3GKeuGpfNJJW0qjHWHgiysNxnT Zthw==
The code setting up the locks is `mdb_env_setup_locks` circa line 4250 of mdb.c. This code has a bunch of #ifdefs for posix vs win32. A major difference is that locks for posix are non-recursive by default (unless you set a specific flag), whereas the CreateMutex operation used for win32 creates recursive locks.
The MDB_NOTLS flag only affects readers.
When two writers operate in the same thread using M:N threading, a bug will occur in Windows whenever two different lightweight 'writer' threads grab the same mutex in the same OS thread. Because both writers will succeed.
Conversely, in posix systems, a different bug will occur: the second writer will attempt to grab the mutex; this will lock up the thread... which, unfortunately, happens to be the same thread that the first writer needs to use to unlock the mutex. There is no way for a lightweight writer to migrate to another thread and unlock it there.
I believe both systems would behave more consistently and usefully if we switch from mutexes to semaphores (of size 1). The difference is that a semaphore doesn't track an 'owner' thread. This is also the suggestion to avoid the recursive mutex behavior of windows .
I would like to hear other opinions on this matter.