summaryrefslogtreecommitdiff
path: root/crypto/cryptlib.c
diff options
context:
space:
mode:
authorGeoff Thorpe <geoff@openssl.org>2008-03-28 02:49:43 +0000
committerGeoff Thorpe <geoff@openssl.org>2008-03-28 02:49:43 +0000
commitf7ccba3edf9f1f02d7bd3b019d7bc96f25a95718 (patch)
tree734b39872b1b8c3cee1db78ed5eb7e3214d6a0a7 /crypto/cryptlib.c
parenteb77ebe26c5228a9a9688b33901e79968789b980 (diff)
downloadopenssl-new-f7ccba3edf9f1f02d7bd3b019d7bc96f25a95718.tar.gz
There was a need to support thread ID types that couldn't be reliably cast
to 'unsigned long' (ie. odd platforms/compilers), so a pointer-typed version was added but it required portable code to check *both* modes to determine equality. This commit maintains the availability of both thread ID types, but deprecates the type-specific accessor APIs that invoke the callbacks - instead a single type-independent API is used. This simplifies software that calls into this interface, and should also make it less error-prone - as forgetting to call and compare *both* thread ID accessors could have led to hard-to-debug/infrequent bugs (that might only affect certain platforms or thread implementations). As the CHANGES note says, there were corresponding deprecations and replacements in the thread-related functions for BN_BLINDING and ERR too.
Diffstat (limited to 'crypto/cryptlib.c')
-rw-r--r--crypto/cryptlib.c96
1 files changed, 62 insertions, 34 deletions
diff --git a/crypto/cryptlib.c b/crypto/cryptlib.c
index 33c70afa33..73bdfd8ce9 100644
--- a/crypto/cryptlib.c
+++ b/crypto/cryptlib.c
@@ -415,16 +415,61 @@ void CRYPTO_set_add_lock_callback(int (*func)(int *num,int mount,int type,
add_lock_callback=func;
}
-unsigned long (*CRYPTO_get_id_callback(void))(void)
- {
- return(id_callback);
- }
+/* Thread IDs. So ... if we build without OPENSSL_NO_DEPRECATED, then we leave
+ * the existing implementations and just layer CRYPTO_THREADID_[get|cmp]
+ * harmlessly on top. Otherwise, we only use 'id_callback' or 'idptr_callback'
+ * if they're non-NULL, ie. we ignore CRYPTO_thread_id()'s fallbacks and we
+ * move CRYPTO_thread_idptr()'s "&errno" fallback trick into
+ * CRYPTO_THREADID_set(). */
void CRYPTO_set_id_callback(unsigned long (*func)(void))
{
id_callback=func;
}
+void CRYPTO_set_idptr_callback(void *(*func)(void))
+ {
+ idptr_callback=func;
+ }
+
+void CRYPTO_THREADID_set(CRYPTO_THREADID *id)
+ {
+ memset(id, 0, sizeof(*id));
+ if (idptr_callback)
+ id->ptr = idptr_callback();
+ else if (id_callback)
+ id->ulong = id_callback();
+ else
+ id->ptr = &errno;
+ }
+
+int CRYPTO_THREADID_cmp(const CRYPTO_THREADID *id1, const CRYPTO_THREADID *id2)
+ {
+ if (id1->ptr != id2->ptr)
+ return ((id1->ptr < id2->ptr) ? -1 : 1);
+ if (id1->ulong != id2->ulong)
+ return ((id1->ulong < id2->ulong ) ? -1 : 1);
+ return 0;
+ }
+
+unsigned long CRYPTO_THREADID_hash(const CRYPTO_THREADID *id)
+ {
+ if (idptr_callback || !id_callback)
+ return (unsigned long)id->ptr;
+ return id->ulong;
+ }
+
+void CRYPTO_THREADID_cpy(CRYPTO_THREADID *dst, const CRYPTO_THREADID *src)
+ {
+ memcpy(dst, src, sizeof(*src));
+ }
+
+#ifndef OPENSSL_NO_DEPRECATED
+unsigned long (*CRYPTO_get_id_callback(void))(void)
+ {
+ return(id_callback);
+ }
+
unsigned long CRYPTO_thread_id(void)
{
unsigned long ret=0;
@@ -447,34 +492,14 @@ unsigned long CRYPTO_thread_id(void)
ret=id_callback();
return(ret);
}
-
-void *(*CRYPTO_get_idptr_callback(void))(void)
- {
- return(idptr_callback);
- }
-
-void CRYPTO_set_idptr_callback(void *(*func)(void))
- {
- idptr_callback=func;
- }
-
-void *CRYPTO_thread_idptr(void)
- {
- void *ret=NULL;
-
- if (idptr_callback == NULL)
- ret = &errno;
- else
- ret = idptr_callback();
-
- return ret;
- }
+#endif
void CRYPTO_lock(int mode, int type, const char *file, int line)
{
#ifdef LOCK_DEBUG
{
char *rw_text,*operation_text;
+ CRYPTO_THREADID tid;
if (mode & CRYPTO_LOCK)
operation_text="lock ";
@@ -490,8 +515,9 @@ void CRYPTO_lock(int mode, int type, const char *file, int line)
else
rw_text="ERROR";
- fprintf(stderr,"lock:%08lx/%08p:(%s)%s %-18s %s:%d\n",
- CRYPTO_thread_id(), CRYPTO_thread_idptr(), rw_text, operation_text,
+ CRYPTO_THREADID_set(&tid);
+ fprintf(stderr,"lock:%08lx:(%s)%s %-18s %s:%d\n",
+ CRYPTO_THREADID_hash(&tid), rw_text, operation_text,
CRYPTO_get_lock_name(type), file, line);
}
#endif
@@ -518,6 +544,10 @@ int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
int line)
{
int ret = 0;
+#ifdef LOCK_DEBUG
+ CRYPTO_THREADID tid;
+ CRYPTO_THREADID_set(&tid);
+#endif
if (add_lock_callback != NULL)
{
@@ -527,9 +557,8 @@ int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
ret=add_lock_callback(pointer,amount,type,file,line);
#ifdef LOCK_DEBUG
- fprintf(stderr,"ladd:%08lx/%0xp:%2d+%2d->%2d %-18s %s:%d\n",
- CRYPTO_thread_id(), CRYPTO_thread_idptr(),
- before,amount,ret,
+ fprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
+ CRYPTO_THREADID_hash(&tid), before,amount,ret,
CRYPTO_get_lock_name(type),
file,line);
#endif
@@ -540,9 +569,8 @@ int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
ret= *pointer+amount;
#ifdef LOCK_DEBUG
- fprintf(stderr,"ladd:%08lx/%0xp:%2d+%2d->%2d %-18s %s:%d\n",
- CRYPTO_thread_id(), CRYPTO_thread_idptr(),
- *pointer,amount,ret,
+ fprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
+ CRYPTO_THREADID_hash(&tid), *pointer,amount,ret,
CRYPTO_get_lock_name(type),
file,line);
#endif