Commit 299c49ee authored by Marek Vavrusa's avatar Marek Vavrusa

Reformatted rest of the server.

parent f97847f0
......@@ -414,10 +414,10 @@ int dt_resize(dt_unit_t *unit, int size)
// Realloc threads
debug_dt("dt_resize: growing from %d to %d threads\n",
unit->size, size);
unit->size, size);
dthread_t **threads = realloc(unit->threads,
size * sizeof(dthread_t *));
size * sizeof(dthread_t *));
if (threads == 0) {
return -1;
}
......@@ -484,7 +484,7 @@ int dt_resize(dt_unit_t *unit, int size)
// Invalidate in old vector
unit->threads[i] = 0;
debug_dt("dthreads: [%p] dt_resize: elected\n",
thread);
thread);
} else if (remaining <= 0) {
......@@ -499,7 +499,7 @@ int dt_resize(dt_unit_t *unit, int size)
thread->state = ThreadDead | ThreadCancelled;
dt_signalize(thread, SIGALRM);
debug_dt("dthreads: [%p] dt_resize: "
"is discarded\n", thread);
"is discarded\n", thread);
}
// Unlock thread and continue
......@@ -573,13 +573,13 @@ int dt_start(dt_unit_t *unit)
int res = dt_start_id(thread);
if (res != 0) {
log_error("dthreads: %s: failed to create thread %d",
__func__, i);
__func__, i);
dt_unit_unlock(unit);
return res;
}
debug_dt("dthreads: [%p] %s: thread started\n",
thread, __func__);
thread, __func__);
}
// Unlock unit
......@@ -609,16 +609,16 @@ int dt_start_id(dthread_t *thread)
// Do not re-create running threads
if (prev_state != ThreadJoined) {
debug_dt("dthreads: [%p] %s: refused to recreate thread\n",
thread, __func__);
thread, __func__);
unlock_thread_rw(thread);
return 0;
}
// Start thread
int res = pthread_create(&thread->_thr, /* pthread_t */
&thread->_attr, /* pthread_attr_t */
thread_ep, /* routine: thread_ep */
thread); /* passed object: dthread_t */
&thread->_attr, /* pthread_attr_t */
thread_ep, /* routine: thread_ep */
thread); /* passed object: dthread_t */
// Unlock thread
unlock_thread_rw(thread);
......@@ -663,10 +663,10 @@ int dt_join(dt_unit_t *unit)
if (thread->state & ThreadJoinable) {
unlock_thread_rw(thread);
debug_dt("dthreads: [%p] %s: reclaiming\n",
thread, __func__);
thread, __func__);
pthread_join(thread->_thr, 0);
debug_dt("dthreads: [%p] %s: reclaimed\n",
thread, __func__);
thread, __func__);
thread->state = ThreadJoined;
} else {
unlock_thread_rw(thread);
......@@ -736,7 +736,7 @@ int dt_stop(dt_unit_t *unit)
if (thread->state & (ThreadIdle | ThreadActive)) {
thread->state = ThreadDead | ThreadCancelled;
debug_dt("dthreads: [%p] %s: stopping thread\n",
thread, __func__);
thread, __func__);
dt_signalize(thread, SIGALRM);
}
unlock_thread_rw(thread);
......@@ -777,7 +777,7 @@ int dt_setprio(dthread_t *thread, int prio)
// Report
if (ret < 0) {
debug_dt("dthreads: [%p] %s(%d): failed: %s",
thread, __func__, prio, strerror(errno));
thread, __func__, prio, strerror(errno));
}
return ret;
......@@ -871,11 +871,11 @@ int dt_compact(dt_unit_t *unit)
lock_thread_rw(thread);
if (thread->state & (ThreadDead)) {
debug_dt("dthreads: [%p] %s: reclaiming thread\n",
__func__, thread);
__func__, thread);
unlock_thread_rw(thread);
pthread_join(thread->_thr, 0);
debug_dt("dthreads: [%p] %s: thread reclaimed\n",
__func__, thread);
__func__, thread);
thread->state = ThreadJoined;
} else {
unlock_thread_rw(thread);
......
/*!
* \file dthreads.h
* \author Marek Vavrusa <marek.vavrusa@nic.cz>
* \author Marek Vavrusa <marek.vavusa@nic.cz>
*
* \brief Threading API.
*
......@@ -16,7 +16,7 @@
* Incoherent function is when at least one thread executes
* a different runnable than the others.
*
* \addtogroup threading
* \addtogroup server
* @{
*/
......@@ -95,8 +95,8 @@ typedef struct dt_unit_t {
*
* \param count Requested thread count.
*
* \retval new instance if successful
* \retval 0 if some error occured
* \retval New instance if successful
* \retval NULL on error
*/
dt_unit_t *dt_create(int count);
......@@ -109,8 +109,8 @@ dt_unit_t *dt_create(int count);
* \param runnable Runnable function for all threads.
* \param data Any data passed onto threads.
*
* \retval new instance if successful
* \retval 0 if some error occured
* \retval New instance if successful
* \retval NULL on error
*/
dt_unit_t *dt_create_coherent(int count, runnable_t runnable, void *data);
......@@ -140,8 +140,8 @@ void dt_delete(dt_unit_t **unit);
* \param unit Unit to be resized.
* \param size New unit size.
*
* \retval 0 on success
* \retval <0 if some error occured
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int dt_resize(dt_unit_t *unit, int size);
......@@ -150,8 +150,8 @@ int dt_resize(dt_unit_t *unit, int size);
*
* \param unit Unit to be started.
*
* \retval 0 on success
* \retval <0 if some error occured
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int dt_start(dt_unit_t *unit);
......@@ -160,8 +160,8 @@ int dt_start(dt_unit_t *unit);
*
* \param thread Target thread instance.
*
* \retval 0 on success
* \retval <0 if some error occured
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int dt_start_id(dthread_t *thread);
......@@ -175,8 +175,8 @@ int dt_start_id(dthread_t *thread);
* \param thread Target thread instance.
* \param signum Signal code.
*
* \retval 0 on success
* \retval <0 if some error occured
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int dt_signalize(dthread_t *thread, int signum);
......@@ -185,8 +185,8 @@ int dt_signalize(dthread_t *thread, int signum);
*
* \param unit Unit to be joined.
*
* \retval 0 on success
* \retval <0 if some error occured
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int dt_join(dt_unit_t *unit);
......@@ -197,8 +197,8 @@ int dt_join(dt_unit_t *unit);
*
* \param thread Target thread instance.
*
* \retval 0 on success
* \retval <0 if some error occured
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int dt_stop_id(dthread_t *thread);
......@@ -209,8 +209,8 @@ int dt_stop_id(dthread_t *thread);
*
* \param unit Unit to be stopped.
*
* \retval 0 on success
* \retval <0 if some error occured
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int dt_stop(dt_unit_t *unit);
......@@ -220,8 +220,8 @@ int dt_stop(dt_unit_t *unit);
* \param thread Target thread instance.
* \param prio Requested priority (positive integer, default is 0).
*
* \retval 0 on success
* \retval <0 if some error occured
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int dt_setprio(dthread_t *thread, int prio);
......@@ -232,8 +232,8 @@ int dt_setprio(dthread_t *thread, int prio);
* \param runnable Runnable function for target thread.
* \param data Data passed to target thread.
*
* \retval 0 on success
* \retval <0 if some error occured
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int dt_repurpose(dthread_t *thread, runnable_t runnable, void *data);
......@@ -248,8 +248,8 @@ int dt_repurpose(dthread_t *thread, runnable_t runnable, void *data);
*
* \param thread Target thread instance.
*
* \retval 0 on success
* \retval <0 if some error occured
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int dt_activate(dthread_t *thread);
......@@ -264,8 +264,8 @@ int dt_activate(dthread_t *thread);
*
* \param thread Target thread instance.
*
* \retval 0 on success
* \retval <0 if some error occured
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int dt_cancel(dthread_t *thread);
......@@ -274,8 +274,8 @@ int dt_cancel(dthread_t *thread);
*
* \param unit Target unit instance.
*
* \retval 0 on success
* \retval <0 if some error occured
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int dt_compact(dt_unit_t *unit);
......@@ -285,7 +285,7 @@ int dt_compact(dt_unit_t *unit);
* It is estimated as NUM_CPUs + 1.
* Fallback is DEFAULT_THR_COUNT (\see common.h).
*
* \return number of threads
* \return Number of threads.
*/
int dt_optimal_size();
......@@ -296,8 +296,8 @@ int dt_optimal_size();
*
* \param thread Target thread instance.
*
* \retval logical true on success
* \retval logical false if not cancelled
* \retval Logical true if cancelled.
* \retval Logical false if not cancelled.
*/
int dt_is_cancelled(dthread_t *thread);
......@@ -307,8 +307,8 @@ int dt_is_cancelled(dthread_t *thread);
*
* \param unit Target unit instance.
*
* \retval 0 on success
* \retval <0 if some error occured
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int dt_unit_lock(dt_unit_t *unit);
......@@ -319,11 +319,12 @@ int dt_unit_lock(dt_unit_t *unit);
*
* \param unit Target unit instance.
*
* \retval 0 on success
* \retval <0 if some error occured
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int dt_unit_unlock(dt_unit_t *unit);
#endif // _CUTEDNS_DTHREADS_H_
/** @} */
/*! @} */
......@@ -10,10 +10,11 @@
cute_server *cute_create()
{
// Create server structure
debug_server("Creating Server structure..\n");
cute_server *server = malloc(sizeof(cute_server));
server->handlers = NULL;
server->state = Idle;
server->state = ServerIdle;
if (server == NULL) {
ERR_ALLOC_FAILED;
return NULL;
......@@ -42,7 +43,8 @@ cute_server *cute_create()
// Estimate number of threads/manager
int thr_count = dt_optimal_size();
debug_server("Estimated number of threads per handler: %d\n", thr_count);
debug_server("Estimated number of threads per handler: %d\n",
thr_count);
// Create socket handlers
int sock = socket_create(PF_INET, SOCK_STREAM);
......@@ -66,22 +68,23 @@ cute_server *cute_create()
return server;
}
iohandler_t* cute_create_handler(cute_server *server, int fd, dt_unit_t* unit)
iohandler_t *cute_create_handler(cute_server *server, int fd, dt_unit_t *unit)
{
// Create new worker
iohandler_t* handler = malloc(sizeof(iohandler_t));
if(handler == 0)
iohandler_t *handler = malloc(sizeof(iohandler_t));
if (handler == 0) {
return 0;
}
// Initialize
handler->fd = fd;
handler->state = Idle;
handler->state = ServerIdle;
handler->next = server->handlers;
handler->server = server;
handler->unit = unit;
// Update unit data object
for(int i = 0; i < unit->size; ++i) {
for (int i = 0; i < unit->size; ++i) {
dthread_t *thread = unit->threads[i];
dt_repurpose(thread, thread->run, handler);
}
......@@ -90,7 +93,7 @@ iohandler_t* cute_create_handler(cute_server *server, int fd, dt_unit_t* unit)
server->handlers = handler;
// Run if server is online
if(server->state & Running) {
if (server->state & ServerRunning) {
dt_start(handler->unit);
}
......@@ -101,16 +104,15 @@ int cute_remove_handler(cute_server *server, iohandler_t *ref)
{
// Find worker
iohandler_t *w = 0, *p = 0;
for(w = server->handlers; w != NULL; p = w,w = w->next) {
for (w = server->handlers; w != NULL; p = w, w = w->next) {
// Compare fd
if(w == ref) {
if (w == ref) {
// Disconnect
if(p == 0) {
if (p == 0) {
server->handlers = w->next;
}
else {
} else {
p->next = w->next;
}
break;
......@@ -118,13 +120,13 @@ int cute_remove_handler(cute_server *server, iohandler_t *ref)
}
// Check
if(w == 0) {
if (w == 0) {
return -1;
}
// Wait for dispatcher to finish
if(w->state & Running) {
w->state = Idle;
if (w->state & ServerRunning) {
w->state = ServerIdle;
dt_stop(w->unit);
dt_join(w->unit);
}
......@@ -138,7 +140,7 @@ int cute_remove_handler(cute_server *server, iohandler_t *ref)
return 0;
}
int cute_start( cute_server *server, char **filenames, uint zones )
int cute_start(cute_server *server, char **filenames, uint zones)
{
debug_server("Starting server with %u zone files.\n", zones);
//stat
......@@ -158,9 +160,9 @@ int cute_start( cute_server *server, char **filenames, uint zones )
// Start dispatchers
int ret = 0;
server->state |= Running;
for(iohandler_t* w = server->handlers; w != NULL; w = w->next) {
w->state = Running;
server->state |= ServerRunning;
for (iohandler_t *w = server->handlers; w != NULL; w = w->next) {
w->state = ServerRunning;
ret += dt_start(w->unit);
}
......@@ -171,8 +173,9 @@ int cute_wait(cute_server *server)
{
// Wait for dispatchers to finish
int ret = 0;
while(server->handlers != NULL) {
debug_server("server: [%p] joining threading unit\n", server->handlers);
while (server->handlers != NULL) {
debug_server("server: [%p] joining threading unit\n",
server->handlers);
ret += dt_join(server->handlers->unit);
cute_remove_handler(server, server->handlers);
debug_server("server: joined threading unit\n", p);
......@@ -181,22 +184,22 @@ int cute_wait(cute_server *server)
return ret;
}
void cute_stop( cute_server *server )
void cute_stop(cute_server *server)
{
// Notify servers to stop
server->state &= ~Running;
for(iohandler_t* w = server->handlers; w != NULL; w = w->next) {
w->state = Idle;
server->state &= ~ServerRunning;
for (iohandler_t *w = server->handlers; w != NULL; w = w->next) {
w->state = ServerIdle;
dt_stop(w->unit);
}
}
void cute_destroy( cute_server **server )
void cute_destroy(cute_server **server)
{
// Free workers
iohandler_t* w = (*server)->handlers;
while(w != NULL) {
iohandler_t* n = w->next;
iohandler_t *w = (*server)->handlers;
while (w != NULL) {
iohandler_t *n = w->next;
cute_remove_handler(*server, w);
w = n;
}
......
/*!
* @file server.h
* \file server.h
* \author Lubos Slovak <lubos.slovak@nic.cz>
*
* \brief Core server functions.
*
* Contains the main high-level server structure (cute_server) and interface
* to functions taking care of proper initialization of the server and clean-up
......@@ -8,10 +11,12 @@
* As of now, the server supports only one zone file and only in a special
* format.
*
* @see zone-parser.h
* \see zone-parser.h
* \addtogroup server
* @{
*/
#ifndef SERVER_H
#define SERVER_H
#ifndef _CUTEDNS_SERVER_H_
#define _CUTEDNS_SERVER_H_
#include "zone-database.h"
#include "name-server.h"
......@@ -19,58 +24,62 @@
#include "socket.h"
#include "dthreads.h"
/** I/O handler structure.
/*! \brief I/O handler structure.
*/
typedef struct iohandler_t {
int fd; /* I/O filedescripto r */
unsigned state; /* Handler state */
struct iohandler_t* next; /* Next handler */
dt_unit_t* unit; /* Threading unit */
struct cute_server* server; /* Reference to server */
int fd; /*!< I/O filedescriptor */
unsigned state; /*!< Handler state */
struct iohandler_t *next; /*!< Next handler */
dt_unit_t *unit; /*!< Threading unit */
struct cute_server *server; /*!< Reference to server */
} iohandler_t;
/*! Round-robin mechanism of switching.
/*! \brief Round-robin mechanism of switching.
*/
#define get_next_rr(current, count) \
(((current) + 1) % (count))
(((current) + 1) % (count))
/*! Server state flags.
/*! \brief Server state flags.
*/
typedef enum {
Idle = 0 << 0,
Running = 1 << 0
ServerIdle = 0 << 0, /*!< Server is idle. */
ServerRunning = 1 << 0 /*!< Server is running. */
} server_state;
/* Forwad declarations. */
struct cute_server;
/*! Forwad declaration of opaque I/O handler. */
struct iohandler_t;
/*!
* @brief Main server structure. Keeps references to all important structures
* needed for operation.
* \brief Main server structure.
*
* Keeps references to all important structures needed for operation.
*/
typedef struct cute_server {
/*! @brief Server state tracking. */
/*! \brief Server state tracking. */
unsigned state;
/*! @brief Reference to the name server structure. */
/*! \brief Reference to the name server structure. */
ns_nameserver *nameserver;
/*! @brief Reference to the zone database structure. */
/*! \brief Reference to the zone database structure. */
zdb_database *zone_db;
/*! @brief I/O handlers list. */
/*! \brief I/O handlers list. */
struct iohandler_t *handlers;
} cute_server;
/*!
* @brief Allocates and initializes the server structure. Creates all other
* main structures
* \brief Allocates and initializes the server structure.
*
* Creates all other main structures.
*
* \retval New instance if successful.
* \retval 0 If an error occured.
*/
cute_server *cute_create();
......@@ -80,53 +89,67 @@ cute_server *cute_create();
* Pointer to handler instance is used as native unique identifier.
* This requests instance not to be reallocated.
*
* \param server Server structure to be used for operation.
* \param fd I/O filedescriptor.
* \param unit Threading unit to serve given filedescriptor.
* \return handler identifier or 0
*
* \retval Handler instance if successful.
* \retval 0 If an error occured.
*/
iohandler_t* cute_create_handler(cute_server *server, int fd, dt_unit_t* unit);
iohandler_t *cute_create_handler(cute_server *server, int fd, dt_unit_t *unit);
/*!
* \brief Delete handler.
*
* \param fd I/O handler filedescriptor.
* \return >=0 If successful, negative integer on failure.
*
* \param server Server structure to be used for operation.
* \param ref I/O handler instance.
*
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int cute_remove_handler(cute_server *server, iohandler_t *ref);
/*!
* @brief Starts the server.
* \brief Starts the server.
*
* @param server Server structure to be used for operation.
* @param filename Zone file name to be used by the server.
* \param server Server structure to be used for operation.
* \param filename Zone file name to be used by the server.
*
* @retval 0 On success.
* @retval -1 If an error occured.
* \retval 0 On success.
* \retval <0 If an error occured.
*
* @todo When a module for configuration is added, the filename parameter will
* \todo When a module for configuration is added, the filename parameter will
* be removed.
*/
int cute_start(cute_server *server, char **filenames, uint zones);
/*!
* @brief Waits for the server to finish.
*
* @param server Server structure to be used for operation.
* \brief Waits for the server to finish.
*
* @retval 0 On success.
* @retval -1 If an error occured.
* \param server Server structure to be used for operation.
*
* \retval 0 On success.
* \retval <0 If an error occured.
*/
int cute_wait(cute_server *server);
/*!
* @brief Requests server to stop.
* \brief Requests server to stop.
*
* \param server Server structure to be used for operation.
*/
void cute_stop( cute_server *server );
void cute_stop(cute_server *server);
/*!
* @brief Properly destroys the server structure.
* \brief Properly destroys the server structure.
*
* \param server Server structure to be used for operation.
*/
void cute_destroy( cute_server **server );