added Window-isms previously in gl.h
[mesa.git] / src / mesa / main / hash.c
index a7364b4dac5bf2658e7ba809fdff70bd7ebf1698..17bcd1c5f3e62eb605a66b3f13659d25090d319e 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: hash.c,v 1.6 2000/01/24 16:19:54 brianp Exp $ */
+/* $Id: hash.c,v 1.9 2000/03/21 22:20:42 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
@@ -29,6 +29,7 @@
 #include "all.h"
 #else
 #include "glheader.h"
+#include "glthread.h"
 #include "hash.h"
 #include "mem.h"
 #endif
@@ -53,6 +54,7 @@ struct HashEntry {
 struct _mesa_HashTable {
    struct HashEntry *Table[TABLE_SIZE];
    GLuint MaxKey;
+   _glthread_Mutex Mutex;
 };
 
 
@@ -130,6 +132,8 @@ void _mesa_HashInsert(struct _mesa_HashTable *table, GLuint key, void *data)
    assert(table);
    assert(key);
 
+   _glthread_LOCK_MUTEX(table->Mutex);
+
    if (key > table->MaxKey)
       table->MaxKey = key;
 
@@ -139,6 +143,7 @@ void _mesa_HashInsert(struct _mesa_HashTable *table, GLuint key, void *data)
       if (entry->Key == key) {
          /* replace entry's data */
         entry->Data = data;
+         _glthread_UNLOCK_MUTEX(table->Mutex);
         return;
       }
       entry = entry->Next;
@@ -150,6 +155,8 @@ void _mesa_HashInsert(struct _mesa_HashTable *table, GLuint key, void *data)
    entry->Data = data;
    entry->Next = table->Table[pos];
    table->Table[pos] = entry;
+
+   _glthread_UNLOCK_MUTEX(table->Mutex);
 }
 
 
@@ -167,6 +174,8 @@ void _mesa_HashRemove(struct _mesa_HashTable *table, GLuint key)
    assert(table);
    assert(key);
 
+   _glthread_LOCK_MUTEX(table->Mutex);
+
    pos = key & (TABLE_SIZE-1);
    prev = NULL;
    entry = table->Table[pos];
@@ -180,28 +189,35 @@ void _mesa_HashRemove(struct _mesa_HashTable *table, GLuint key)
             table->Table[pos] = entry->Next;
          }
          FREE(entry);
+         _glthread_UNLOCK_MUTEX(table->Mutex);
         return;
       }
       prev = entry;
       entry = entry->Next;
    }
+
+   _glthread_UNLOCK_MUTEX(table->Mutex);
 }
 
 
 
 /*
  * Return the key of the "first" entry in the hash table.
- * By calling this function until zero is returned we can get
- * the keys of all entries in the table.
+ * This is used in the course of deleting all display lists when
+ * a context is destroyed.
  */
-GLuint _mesa_HashFirstEntry(const struct _mesa_HashTable *table)
+GLuint _mesa_HashFirstEntry(struct _mesa_HashTable *table)
 {
    GLuint pos;
    assert(table);
+   _glthread_LOCK_MUTEX(table->Mutex);
    for (pos=0; pos < TABLE_SIZE; pos++) {
-      if (table->Table[pos])
+      if (table->Table[pos]) {
+         _glthread_UNLOCK_MUTEX(table->Mutex);
          return table->Table[pos]->Key;
+      }
    }
+   _glthread_UNLOCK_MUTEX(table->Mutex);
    return 0;
 }
 
@@ -231,11 +247,13 @@ void _mesa_HashPrint(const struct _mesa_HashTable *table)
  *         numKeys - number of keys needed
  * Return:  starting key of free block or 0 if failure
  */
-GLuint _mesa_HashFindFreeKeyBlock(const struct _mesa_HashTable *table, GLuint numKeys)
+GLuint _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable *table, GLuint numKeys)
 {
    GLuint maxKey = ~((GLuint) 0);
+   _glthread_LOCK_MUTEX(table->Mutex);
    if (maxKey - numKeys > table->MaxKey) {
       /* the quick solution */
+      _glthread_UNLOCK_MUTEX(table->Mutex);
       return table->MaxKey + 1;
    }
    else {
@@ -253,11 +271,13 @@ GLuint _mesa_HashFindFreeKeyBlock(const struct _mesa_HashTable *table, GLuint nu
            /* this key not in use, check if we've found enough */
            freeCount++;
            if (freeCount == numKeys) {
+               _glthread_UNLOCK_MUTEX(table->Mutex);
               return freeStart;
            }
         }
       }
       /* cannot allocate a block of numKeys consecutive keys */
+      _glthread_UNLOCK_MUTEX(table->Mutex);
       return 0;
    }
 }