Get rid of the THREADID stuff.

Now that ERR is using thread-local storage, there's very little that the
THREADID code is doing and it can be turned into stub functions.

Change-Id: I668613fec39b26c894d029b10a8173c3055f6019
diff --git a/crypto/err/err.c b/crypto/err/err.c
index 521faed..2552586 100644
--- a/crypto/err/err.c
+++ b/crypto/err/err.c
@@ -587,16 +587,15 @@
 }
 
 void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
-  CRYPTO_THREADID current_thread;
   char buf[ERR_ERROR_STRING_BUF_LEN];
   char buf2[1024];
-  unsigned long thread_hash;
   const char *file, *data;
   int line, flags;
   uint32_t packed_error;
 
-  CRYPTO_THREADID_current(&current_thread);
-  thread_hash = CRYPTO_THREADID_hash(&current_thread);
+  /* thread_hash is the least-significant bits of the |ERR_STATE| pointer value
+   * for this thread. */
+  const unsigned long thread_hash = (uintptr_t) err_get_state();
 
   for (;;) {
     packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
diff --git a/crypto/rsa/blinding.c b/crypto/rsa/blinding.c
index 6b13d0d..16c34bb 100644
--- a/crypto/rsa/blinding.c
+++ b/crypto/rsa/blinding.c
@@ -124,7 +124,6 @@
   BIGNUM *Ai;
   BIGNUM *e;
   BIGNUM *mod; /* just a reference */
-  CRYPTO_THREADID tid;
   int counter;
   unsigned long flags;
   BN_MONT_CTX *m_ctx;
@@ -167,7 +166,6 @@
    * to indicate that this is never-used fresh blinding
    * that does not need updating before first use. */
   ret->counter = -1;
-  CRYPTO_THREADID_current(&ret->tid);
   return ret;
 
 err:
@@ -286,8 +284,6 @@
   return ret;
 }
 
-CRYPTO_THREADID *BN_BLINDING_thread_id(BN_BLINDING *b) { return &b->tid; }
-
 unsigned long BN_BLINDING_get_flags(const BN_BLINDING *b) { return b->flags; }
 
 void BN_BLINDING_set_flags(BN_BLINDING *b, unsigned long flags) {
@@ -460,7 +456,6 @@
     OPENSSL_PUT_ERROR(RSA, rsa_setup_blinding, ERR_R_BN_LIB);
     goto err;
   }
-  CRYPTO_THREADID_current(BN_BLINDING_thread_id(ret));
 
 err:
   BN_CTX_end(ctx);
diff --git a/crypto/rsa/internal.h b/crypto/rsa/internal.h
index 3dd4f04..2763253 100644
--- a/crypto/rsa/internal.h
+++ b/crypto/rsa/internal.h
@@ -81,7 +81,6 @@
 int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
 int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *);
 int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *);
-CRYPTO_THREADID *BN_BLINDING_thread_id(BN_BLINDING *);
 unsigned long BN_BLINDING_get_flags(const BN_BLINDING *);
 void BN_BLINDING_set_flags(BN_BLINDING *, unsigned long);
 BN_BLINDING *BN_BLINDING_create_param(
diff --git a/crypto/rsa/rsa_impl.c b/crypto/rsa/rsa_impl.c
index 5927126..064513b 100644
--- a/crypto/rsa/rsa_impl.c
+++ b/crypto/rsa/rsa_impl.c
@@ -218,31 +218,14 @@
   char overflow = 0;
 
   CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING);
-  if (rsa->num_blindings > 0) {
-    unsigned i, starting_index;
-    CRYPTO_THREADID threadid;
 
-    /* We start searching the array at a value based on the
-     * threadid in order to try avoid bouncing the BN_BLINDING
-     * values around different threads. It's harmless if
-     * threadid.val is always set to zero. */
-    CRYPTO_THREADID_current(&threadid);
-    starting_index = threadid.val % rsa->num_blindings;
-
-    for (i = starting_index;;) {
-      if (rsa->blindings_inuse[i] == 0) {
-        rsa->blindings_inuse[i] = 1;
-        ret = rsa->blindings[i];
-        *index_used = i;
-        break;
-      }
-      i++;
-      if (i == rsa->num_blindings) {
-        i = 0;
-      }
-      if (i == starting_index) {
-        break;
-      }
+  unsigned i;
+  for (i = 0; i < rsa->num_blindings; i++) {
+    if (rsa->blindings_inuse[i] == 0) {
+      rsa->blindings_inuse[i] = 1;
+      ret = rsa->blindings[i];
+      *index_used = i;
+      break;
     }
   }
 
diff --git a/crypto/thread.c b/crypto/thread.c
index 5101617..48d5bbc 100644
--- a/crypto/thread.c
+++ b/crypto/thread.c
@@ -83,8 +83,6 @@
                                 int line) = 0;
 static int (*add_lock_callback)(int *pointer, int amount, int lock_num,
                                 const char *file, int line) = 0;
-static void (*threadid_callback)(CRYPTO_THREADID *) = 0;
-
 
 int CRYPTO_num_locks(void) { return CRYPTO_NUM_LOCKS; }
 
@@ -106,23 +104,13 @@
   }
 }
 
-int CRYPTO_THREADID_set_callback(void (*func)(CRYPTO_THREADID *)) {
-  if (threadid_callback) {
-    return 0;
-  }
-  threadid_callback = func;
-  return 1;
-}
+int CRYPTO_THREADID_set_callback(void (*func)(CRYPTO_THREADID *)) { return 1; }
 
-void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val) {
-  memset(id, 0, sizeof(*id));
-  id->val = val;
-}
+void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val) {}
 
-void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr) {
-  memset(id, 0, sizeof(*id));
-  id->ptr = ptr;
-}
+void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr) {}
+
+void CRYPTO_THREADID_current(CRYPTO_THREADID *id) {}
 
 void (*CRYPTO_get_locking_callback(void))(int mode, int lock_num,
                                           const char *file, int line) {
@@ -156,32 +144,6 @@
   return ret;
 }
 
-void CRYPTO_THREADID_current(CRYPTO_THREADID *id) {
-  if (threadid_callback) {
-    threadid_callback(id);
-    return;
-  }
-
-#if defined(OPENSSL_WINDOWS)
-  CRYPTO_THREADID_set_numeric(id, (unsigned long)GetCurrentThreadId());
-#else
-  /* For everything else, default to using the address of 'errno' */
-  CRYPTO_THREADID_set_pointer(id, (void *)&errno);
-#endif
-}
-
-int CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a, const CRYPTO_THREADID *b) {
-  return memcmp(a, b, sizeof(*a));
-}
-
-void CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest, const CRYPTO_THREADID *src) {
-  memcpy(dest, src, sizeof(*src));
-}
-
-uint32_t CRYPTO_THREADID_hash(const CRYPTO_THREADID *id) {
-  return OPENSSL_hash32(id, sizeof(CRYPTO_THREADID));
-}
-
 void CRYPTO_set_id_callback(unsigned long (*func)(void)) {}
 
 void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(
diff --git a/include/openssl/thread.h b/include/openssl/thread.h
index cf08dcc..25e252c 100644
--- a/include/openssl/thread.h
+++ b/include/openssl/thread.h
@@ -67,19 +67,12 @@
 /* Functions to support multithreading.
  *
  * OpenSSL can safely be used in multi-threaded applications provided that at
- * least two callback functions are set with |CRYPTO_set_locking_callback| and
- * |CRYPTO_THREADID_set_callback|.
+ * least |CRYPTO_set_locking_callback| is set.
  *
  * The locking callback performs mutual exclusion. Rather than using a single
  * lock for all, shared data-structures, OpenSSL requires that the locking
  * callback support a fixed (at run-time) number of different locks, given by
- * |CRYPTO_num_locks|.
- *
- * The thread ID callback is called to record the currently executing thread's
- * identifier in a |CRYPTO_THREADID| structure. If this callback is not
- * provided then the address of |errno| is used as the thread identifier. This
- * is sufficient only if the system has a thread-local |errno| value. */
-
+ * |CRYPTO_num_locks|. */
 
 /* CRYPTO_num_locks returns the number of static locks that the callback
  * function passed to |CRYPTO_set_locking_callback| must be able to handle. */
@@ -116,27 +109,25 @@
 OPENSSL_EXPORT const char *CRYPTO_get_lock_name(int lock_num);
 
 
-/* CRYPTO_THREADID identifies a thread in a multithreaded program. This
- * structure should not be used directly. Rather applications should use
- * |CRYPTO_THREADID_set_numeric| and |CRYPTO_THREADID_set_pointer|. */
-typedef struct crypto_threadid_st {
-  void *ptr;
-  unsigned long val;
-} CRYPTO_THREADID;
+/* Deprecated functions */
 
-/* CRYPTO_THREADID_set_callback sets a callback function that stores an
- * identifier of the currently executing thread into |threadid|. The
- * CRYPTO_THREADID structure should not be accessed directly. Rather one of
- * |CRYPTO_THREADID_set_numeric| or |CRYPTO_THREADID_set_pointer| should be
- * used depending on whether thread IDs are numbers or pointers on the host
- * system. */
+/* CRYPTO_THREADID is a dummy value. */
+typedef int CRYPTO_THREADID;
+
+/* CRYPTO_THREADID_set_callback does nothing. */
 OPENSSL_EXPORT int CRYPTO_THREADID_set_callback(
     void (*threadid_func)(CRYPTO_THREADID *threadid));
 
+/* CRYPTO_THREADID_set_numeric does nothing. */
 OPENSSL_EXPORT void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id,
                                                 unsigned long val);
+
+/* CRYPTO_THREADID_set_pointer does nothing. */
 OPENSSL_EXPORT void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr);
 
+/* CRYPTO_THREADID_current does nothing. */
+OPENSSL_EXPORT void CRYPTO_THREADID_current(CRYPTO_THREADID *id);
+
 
 /* Private functions: */
 
@@ -162,20 +153,6 @@
 OPENSSL_EXPORT int CRYPTO_add_lock(int *pointer, int amount, int lock_num,
                                    const char *file, int line);
 
-
-/* CRYPTO_THREADID_current stores the current thread identifier in |id|. */
-OPENSSL_EXPORT void CRYPTO_THREADID_current(CRYPTO_THREADID *id);
-
-/* CRYPTO_THREADID_cmp returns < 0, 0 or > 0 if |a| is less than, equal to or
- * greater than |b|, respectively. */
-int CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a, const CRYPTO_THREADID *b);
-
-/* CRYPTO_THREADID_cpy sets |*dest| equal to |*src|. */
-void CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest, const CRYPTO_THREADID *src);
-
-/* CRYPTO_THREADID_hash returns a hash of the numeric value of |id|. */
-uint32_t CRYPTO_THREADID_hash(const CRYPTO_THREADID *id);
-
 /* Lock IDs start from 1. CRYPTO_LOCK_INVALID_LOCK is an unused placeholder
 * used to ensure no lock has ID 0. */
 #define CRYPTO_LOCK_LIST \