]> mj.ucw.cz Git - libucw.git/blobdiff - lib/fb-direct.c
Merge with git+ssh://cvs.ucw.cz/projects/sherlock/GIT/sherlock.git
[libucw.git] / lib / fb-direct.c
index a4277ec929506c760aead5f8f70dc733536c207d..c3b74e1592edf698ce593e4cca6745afddee6c78 100644 (file)
@@ -19,7 +19,6 @@
  *         and take care of locking.
  *
  *     FIXME: what if the OS doesn't support O_DIRECT?
- *     FIXME: doc: don't mix threads
  *     FIXME: unaligned seeks and partial writes?
  *     FIXME: merge with other file-oriented fastbufs
  */
 #include "lib/lfs.h"
 #include "lib/asio.h"
 #include "lib/conf.h"
+#include "lib/threads.h"
 
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
-#include <pthread.h>
 
 static uns fbdir_cheat;
 static uns fbdir_buffer_size = 65536;
@@ -54,8 +53,6 @@ static struct cf_section fbdir_cf = {
 
 #define FBDIR_ALIGN 512
 
-static pthread_key_t fbdir_queue_key;
-
 enum fbdir_mode {                              // Current operating mode
     M_NULL,
     M_READ,
@@ -80,8 +77,6 @@ static void CONSTRUCTOR
 fbdir_global_init(void)
 {
   cf_declare_section("FBDirect", &fbdir_cf, 0);
-  if (pthread_key_create(&fbdir_queue_key, NULL) < 0)
-    die("Cannot create fbdir_queue_key: %m");
 }
 
 static void
@@ -232,14 +227,15 @@ fbdir_seek(struct fastbuf *f, sh_off_t pos, int whence)
 static struct asio_queue *
 fbdir_get_io_queue(void)
 {
-  struct asio_queue *q = pthread_getspecific(fbdir_queue_key);
+  struct ucwlib_context *ctx = ucwlib_thread_context();
+  struct asio_queue *q = ctx->io_queue;
   if (!q)
     {
       q = xmalloc_zero(sizeof(struct asio_queue));
       q->buffer_size = fbdir_buffer_size;
       q->max_writebacks = fbdir_write_back;
       asio_init_queue(q);
-      pthread_setspecific(fbdir_queue_key, q);
+      ctx->io_queue = q;
     }
   q->use_count++;
   DBG("FB-DIRECT: Got I/O queue, uc=%d", q->use_count);
@@ -249,14 +245,15 @@ fbdir_get_io_queue(void)
 static void
 fbdir_put_io_queue(void)
 {
-  struct asio_queue *q = pthread_getspecific(fbdir_queue_key);
+  struct ucwlib_context *ctx = ucwlib_thread_context();
+  struct asio_queue *q = ctx->io_queue;
   ASSERT(q);
   DBG("FB-DIRECT: Put I/O queue, uc=%d", q->use_count);
   if (!--q->use_count)
     {
       asio_cleanup_queue(q);
       xfree(q);
-      pthread_setspecific(fbdir_queue_key, NULL);
+      ctx->io_queue = NULL;
     }
 }
 
@@ -356,6 +353,18 @@ fbdir_open_fd(int fd, struct asio_queue *q)
   return fbdir_open_internal(x, fd, q);
 }
 
+struct fastbuf *
+fbdir_open_tmp(struct asio_queue *q)
+{
+  byte buf[TEMP_FILE_NAME_LEN];
+  struct fastbuf *f;
+
+  temp_file_name(buf);
+  f = fbdir_open(buf, O_RDWR | O_CREAT | O_TRUNC, q);
+  bconfig(f, BCONFIG_IS_TEMP_FILE, 1);
+  return f;
+}
+
 #ifdef TEST
 
 #include "lib/getopt.h"