1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 herr_t
 ss_aio_write(ss_aio_t *aio)
 {
     SS_ENTER(ss_aio_write, herr_t);

 #if defined(SSLIB_ASYNC_AIO)
     if (aio_write(aio)<0)
         SS_ERROR_FMT(FAILED, ("aio_write failed: %s", strerror(errno)));
 SS_CLEANUP:

 #elif defined(SSLIB_ASYNC_THREADS)
     hbool_t locked=FALSE;

     /* Initialize the aio control struct */
     aio->errnum = EINPROGRESS;
     pthread_mutex_init(&(aio->in_progress), NULL);
     pthread_mutex_lock(&(aio->in_progress));

     /* Append the new request to the queue */
     pthread_mutex_lock(&(ss_aio_g.mutex));
     locked = TRUE;
     if (ss_aio_g.nused>=ss_aio_g.nalloc)
         SS_EXTEND(ss_aio_g.req, MAX(64,ss_aio_g.nused+1), ss_aio_g.nalloc);
     ss_aio_g.req[ss_aio_g.nused++] = aio;
     pthread_cond_signal(&(ss_aio_g.cond));
     pthread_mutex_unlock(&(ss_aio_g.mutex));
 SS_CLEANUP:
     if (locked) pthread_mutex_unlock(&(ss_aio_g.mutex));

 #else
     size_t size = aio->aio_nbytes;
     char *buf = aio->aio_buf;
     if (lseek(aio->aio_fildes, aio->aio_offset, SEEK_SET)<0)
         SS_ERROR_FMT(FAILED, ("lseek failed: %s", strerror(errno)));
     while (size>0) {
         ssize_t n = write(aio->aio_fildes, buf, size);
         if (n<0 && EINTR==errno) continue;
         if (n<0) {
             aio->errnum = errno;
             SS_ERROR_FMT(FAILED, ("write failed: %s", strerror(errno)));
         }
         size -= (size_t)n;
         buf += n;
     }
     if (0==size) aio->errnum = 0;
 SS_CLEANUP:
 #endif

     SS_LEAVE(0);
 }