Threading libsmbclient - a proposal.

Howard Chu hyc at highlandsun.com
Sat Apr 4 17:53:12 GMT 2009


Volker Lendecke wrote:
> On Sat, Apr 04, 2009 at 10:23:26AM -0700, Howard Chu wrote:
>> If you're looking at it from a really low-level view (which I guess you
>> are, since you tried cachegrind) then you have to remember that compiler
>> hints like "unlikely" can't help you all that much. The compiler tries to
>> arrange static branch prediction to keep the most-likely path inline, and
>> branch to the less-likely path. But in this case, it still has to branch
>> around the not-taken path, so the compiler hint doesn't help the code
>> density at all.
>
> We'll have to test all this, for sure.

Stepping back a bit, in the context of the mutex functions, there's another 
consideration. Jeremy was talking about allocating an array of global locks, 
which makes good sense. But if the caller hasn't initialized the mutex 
methods, then that array will most likely be NULL, and calling a dummy mutex 
function unconditionally will still mean dereferencing a NULL pointer to give 
it the expected mutex.

So for sanity's sake, this route is better...

+#define        MUTEX_LOCK(x)   (_tdb_mutex_methods ? 
_tdb_mutex_methods->tmm_lock(x) : 0 )
+#define        MUTEX_UNLOCK(x) (_tdb_mutex_methods ? 
_tdb_mutex_methods->tmm_unlock(x) : 0 )
+#define        MUTEX_TRYLOCK(x)        (_tdb_mutex_methods ? 
_tdb_mutex_methods->tmm_trylock(x) : 0 )

+/* Callbacks that must be set for mutex/thread safety */
+typedef int (tdb_mutex_op)(void * mutex);
+typedef void *(tdb_mutex_create)(int num);
+typedef void (tdb_mutex_destroy)(void *mutex, int num);
+
+struct tdb_mutex_methods {
+       tdb_mutex_create *tmm_create;
+	tdb_mutex_destroy *tmm_destroy;
+       tdb_mutex_op *tmm_lock;
+       tdb_mutex_op *tmm_unlock;
+       tdb_mutex_op *tmm_trylock;
+};
+
+int tdb_set_mutex(struct tdb_mutex_methods *methods);
+struct tdb_mutex_methods *tdb_get_mutex(void);

+struct tdb_mutex_methods *_tdb_mutex_methods = NULL;
+
+int tdb_set_mutex(struct tdb_mutex_methods *methods)
+{
+       if (!methods)
+               return TDB_ERR_EINVAL;
+       if (_tdb_mutex_methods)
+               return TDB_ERR_EXISTS;
+       if (!methods->tmm_create ||
+               !methods->tmm_lock ||
+               !methods->tmm_unlock ||
+               !methods->tmm_trylock ||
+               !methods->tmm_destroy)
+               return TDB_ERR_EINVAL;
+       tdbs_mutex = methods->tmm_create(1);
+       if (!tdbs_mutex)
+               return TDB_ERR_OOM;
+
+       _tdb_mutex_methods = methods;
+
+       return TDB_SUCCESS;
+}



-- 
   -- Howard Chu
   CTO, Symas Corp.           http://www.symas.com
   Director, Highland Sun     http://highlandsun.com/hyc/
   Chief Architect, OpenLDAP  http://www.openldap.org/project/


More information about the samba-technical mailing list