io_channel: Add a thread identifier

This will be used in the future to pass a message
to any given thread.

Change-Id: I3be5fe66244e360b7667427647fd8fdede110930
Signed-off-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-on: https://review.gerrithub.io/362066
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Ben Walker 2017-05-15 15:52:07 -07:00
parent d969ac445a
commit f1c5344b32
3 changed files with 26 additions and 9 deletions

View File

@ -42,6 +42,7 @@
#include "spdk/queue.h"
struct spdk_thread;
struct spdk_io_channel;
typedef int (*io_channel_create_cb_t)(void *io_device, void *ctx_buf);
@ -50,7 +51,7 @@ typedef void (*io_channel_destroy_cb_t)(void *io_device, void *ctx_buf);
/**
* \brief Initializes the calling thread for I/O channel allocation.
*/
void spdk_allocate_thread(void);
struct spdk_thread *spdk_allocate_thread(void);
/**
* \brief Releases any resources related to the calling thread for I/O channel allocation.
@ -60,6 +61,11 @@ void spdk_allocate_thread(void);
*/
void spdk_free_thread(void);
/**
* \brief Get a handle to the current thread.
*/
struct spdk_thread *spdk_get_thread(void);
/**
* \brief Register the opaque io_device context as an I/O device.
*

View File

@ -63,18 +63,29 @@ struct spdk_io_channel {
*/
};
static __thread TAILQ_HEAD(, spdk_io_channel) g_io_channels;
struct spdk_thread {
TAILQ_HEAD(, spdk_io_channel) io_channels;
};
void
static __thread struct spdk_thread g_thread;
struct spdk_thread *
spdk_allocate_thread(void)
{
TAILQ_INIT(&g_io_channels);
TAILQ_INIT(&g_thread.io_channels);
return &g_thread;
}
void
spdk_free_thread(void)
{
assert(TAILQ_EMPTY(&g_io_channels));
assert(TAILQ_EMPTY(&g_thread.io_channels));
}
struct spdk_thread *
spdk_get_thread(void)
{
return &g_thread;
}
void
@ -145,7 +156,7 @@ spdk_get_io_channel(void *io_device)
}
pthread_mutex_unlock(&g_devlist_mutex);
TAILQ_FOREACH(ch, &g_io_channels, tailq) {
TAILQ_FOREACH(ch, &g_thread.io_channels, tailq) {
if (ch->io_device == io_device) {
ch->ref++;
/*
@ -171,7 +182,7 @@ spdk_get_io_channel(void *io_device)
ch->destroy_cb = dev->destroy_cb;
ch->thread_id = pthread_self();
ch->ref = 1;
TAILQ_INSERT_TAIL(&g_io_channels, ch, tailq);
TAILQ_INSERT_TAIL(&g_thread.io_channels, ch, tailq);
return ch;
}
@ -186,7 +197,7 @@ spdk_put_io_channel(struct spdk_io_channel *ch)
ch->ref--;
if (ch->ref == 0) {
TAILQ_REMOVE(&g_io_channels, ch, tailq);
TAILQ_REMOVE(&g_thread.io_channels, ch, tailq);
ch->destroy_cb(ch->io_device, (uint8_t *)ch + sizeof(*ch));
free(ch);
}

View File

@ -144,7 +144,7 @@ channel(void)
spdk_io_device_unregister(&device2);
spdk_io_device_unregister(&device3);
CU_ASSERT(TAILQ_EMPTY(&g_io_devices));
CU_ASSERT(TAILQ_EMPTY(&g_io_channels));
CU_ASSERT(TAILQ_EMPTY(&g_thread.io_channels));
spdk_free_thread();
}