bool pending;
/**
- * Is watcher running?
+ * Running state of watcher
*/
- bool running;
+ watcher_state_t state;
/**
* Lock to access FD list
entry->in_callback = 0;
}
enumerator->destroy(enumerator);
- this->running = FALSE;
+ this->state = WATCHER_STOPPED;
this->condvar->broadcast(this->condvar);
this->mutex->unlock(this->mutex);
}
if (this->fds->get_count(this->fds) == 0)
{
- this->running = FALSE;
+ this->state = WATCHER_STOPPED;
this->mutex->unlock(this->mutex);
return JOB_REQUEUE_NONE;
}
+ if (this->state == WATCHER_QUEUED)
+ {
+ this->state = WATCHER_RUNNING;
+ }
if (this->notify[0] != -1)
{
this->mutex->lock(this->mutex);
this->fds->insert_last(this->fds, entry);
- if (!this->running)
+ if (this->state == WATCHER_STOPPED)
{
- this->running = TRUE;
+ this->state = WATCHER_QUEUED;
lib->processor->queue_job(lib->processor,
(job_t*)callback_job_create_with_prio((void*)watch, this,
NULL, (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
{
if (entry->fd == fd)
{
- if (this->running && entry->in_callback)
+ if (this->state != WATCHER_STOPPED && entry->in_callback)
{
is_in_callback = TRUE;
break;
this->mutex->unlock(this->mutex);
}
+METHOD(watcher_t, get_state, watcher_state_t,
+ private_watcher_t *this)
+{
+ watcher_state_t state;
+
+ this->mutex->lock(this->mutex);
+ state = this->state;
+ this->mutex->unlock(this->mutex);
+
+ return state;
+}
+
METHOD(watcher_t, destroy, void,
private_watcher_t *this)
{
.public = {
.add = _add,
.remove = _remove_,
+ .get_state = _get_state,
.destroy = _destroy,
},
.fds = linked_list_create(),
.condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
.jobs = linked_list_create(),
.notify = {-1, -1},
+ .state = WATCHER_STOPPED,
);
if (!create_notify(this))
typedef struct watcher_t watcher_t;
typedef enum watcher_event_t watcher_event_t;
+typedef enum watcher_state_t watcher_state_t;
#include <library.h>
WATCHER_EXCEPT = (1<<2),
};
+/**
+ * State the watcher currently is in
+ */
+enum watcher_state_t {
+ /** no watcher thread running or queued */
+ WATCHER_STOPPED = 0,
+ /** a job has been queued for watching, but not yet started */
+ WATCHER_QUEUED,
+ /** a watcher thread is active, dispatching socket events */
+ WATCHER_RUNNING,
+};
+
/**
* Watch multiple file descriptors using select().
*/
*/
void (*remove)(watcher_t *this, int fd);
+ /**
+ * Get the current watcher state
+ *
+ * @reutrn currently active watcher state
+ */
+ watcher_state_t (*get_state)(watcher_t *this);
+
/**
* Destroy a watcher_t.
*/