Blame libevent/defer-internal.h

Packit e9ba0d
/*
Packit e9ba0d
 * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
Packit e9ba0d
 *
Packit e9ba0d
 * Redistribution and use in source and binary forms, with or without
Packit e9ba0d
 * modification, are permitted provided that the following conditions
Packit e9ba0d
 * are met:
Packit e9ba0d
 * 1. Redistributions of source code must retain the above copyright
Packit e9ba0d
 *    notice, this list of conditions and the following disclaimer.
Packit e9ba0d
 * 2. Redistributions in binary form must reproduce the above copyright
Packit e9ba0d
 *    notice, this list of conditions and the following disclaimer in the
Packit e9ba0d
 *    documentation and/or other materials provided with the distribution.
Packit e9ba0d
 * 3. The name of the author may not be used to endorse or promote products
Packit e9ba0d
 *    derived from this software without specific prior written permission.
Packit e9ba0d
 *
Packit e9ba0d
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Packit e9ba0d
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit e9ba0d
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit e9ba0d
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit e9ba0d
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit e9ba0d
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit e9ba0d
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit e9ba0d
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit e9ba0d
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit e9ba0d
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit e9ba0d
 */
Packit e9ba0d
#ifndef _DEFER_INTERNAL_H_
Packit e9ba0d
#define _DEFER_INTERNAL_H_
Packit e9ba0d
Packit e9ba0d
#ifdef __cplusplus
Packit e9ba0d
extern "C" {
Packit e9ba0d
#endif
Packit e9ba0d
Packit e9ba0d
#include "event2/event-config.h"
Packit e9ba0d
#include <sys/queue.h>
Packit e9ba0d
Packit e9ba0d
struct deferred_cb;
Packit e9ba0d
Packit e9ba0d
typedef void (*deferred_cb_fn)(struct deferred_cb *, void *);
Packit e9ba0d
Packit e9ba0d
/** A deferred_cb is a callback that can be scheduled to run as part of
Packit e9ba0d
 * an event_base's event_loop, rather than running immediately. */
Packit e9ba0d
struct deferred_cb {
Packit e9ba0d
	/** Links to the adjacent active (pending) deferred_cb objects. */
Packit e9ba0d
	TAILQ_ENTRY (deferred_cb) cb_next;
Packit e9ba0d
	/** True iff this deferred_cb is pending in an event_base. */
Packit e9ba0d
	unsigned queued : 1;
Packit e9ba0d
	/** The function to execute when the callback runs. */
Packit e9ba0d
	deferred_cb_fn cb;
Packit e9ba0d
	/** The function's second argument. */
Packit e9ba0d
	void *arg;
Packit e9ba0d
};
Packit e9ba0d
Packit e9ba0d
/** A deferred_cb_queue is a list of deferred_cb that we can add to and run. */
Packit e9ba0d
struct deferred_cb_queue {
Packit e9ba0d
	/** Lock used to protect the queue. */
Packit e9ba0d
	void *lock;
Packit e9ba0d
Packit e9ba0d
	/** How many entries are in the queue? */
Packit e9ba0d
	int active_count;
Packit e9ba0d
Packit e9ba0d
	/** Function called when adding to the queue from another thread. */
Packit e9ba0d
	void (*notify_fn)(struct deferred_cb_queue *, void *);
Packit e9ba0d
	void *notify_arg;
Packit e9ba0d
Packit e9ba0d
	/** Deferred callback management: a list of deferred callbacks to
Packit e9ba0d
	 * run active the active events. */
Packit e9ba0d
	TAILQ_HEAD (deferred_cb_list, deferred_cb) deferred_cb_list;
Packit e9ba0d
};
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Initialize an empty, non-pending deferred_cb.
Packit e9ba0d
Packit e9ba0d
   @param deferred The deferred_cb structure to initialize.
Packit e9ba0d
   @param cb The function to run when the deferred_cb executes.
Packit e9ba0d
   @param arg The function's second argument.
Packit e9ba0d
 */
Packit e9ba0d
void event_deferred_cb_init(struct deferred_cb *, deferred_cb_fn, void *);
Packit e9ba0d
/**
Packit e9ba0d
   Cancel a deferred_cb if it is currently scheduled in an event_base.
Packit e9ba0d
 */
Packit e9ba0d
void event_deferred_cb_cancel(struct deferred_cb_queue *, struct deferred_cb *);
Packit e9ba0d
/**
Packit e9ba0d
   Activate a deferred_cb if it is not currently scheduled in an event_base.
Packit e9ba0d
 */
Packit e9ba0d
void event_deferred_cb_schedule(struct deferred_cb_queue *, struct deferred_cb *);
Packit e9ba0d
Packit e9ba0d
#define LOCK_DEFERRED_QUEUE(q)						\
Packit e9ba0d
	EVLOCK_LOCK((q)->lock, 0)
Packit e9ba0d
#define UNLOCK_DEFERRED_QUEUE(q)					\
Packit e9ba0d
	EVLOCK_UNLOCK((q)->lock, 0)
Packit e9ba0d
Packit e9ba0d
#ifdef __cplusplus
Packit e9ba0d
}
Packit e9ba0d
#endif
Packit e9ba0d
Packit e9ba0d
void event_deferred_cb_queue_init(struct deferred_cb_queue *);
Packit e9ba0d
struct deferred_cb_queue *event_base_get_deferred_cb_queue(struct event_base *);
Packit e9ba0d
Packit e9ba0d
#endif /* _EVENT_INTERNAL_H_ */
Packit e9ba0d