summaryrefslogblamecommitdiffstats
path: root/src/mpq.h
blob: 70f0dba675c5804cc4330135043bbba30adb152d (plain) (tree)


















                                                                           

                      


                      



                                                

  
                                             
                                        

                                                       

                                                             


                                            
 
                    
                                                                         
                                                                         
                                                                         
                                                                         
                                                                         
                                                                         
                                                                 



                                                                         
                                                                         
                                                                         

                                                                         
 
                    
/*
 * Copyright (c) 2019 Matt Dunwoodie <ncon@noconroy.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _MPQ_H_
#define _MPQ_H_

#include <sys/param.h>
#include <sys/mbuf.h>
#include <sys/systm.h>

struct mpq {
	struct mutex		 mpq_mtx;
	struct mbuf_list	 mpq_list;
	struct mbuf		*mpq_cursor;
	void 			*mpq_serializer;
};

void		 mpq_init(struct mpq *, int);
int		 mpq_full(struct mpq *);
int		 mpq_serialize_try_enter(struct mpq *);
void		 mpq_serialize_leave(struct mpq *);
void		 mpq_threaddone(struct mpq *, struct mbuf *);
void		 mpq_enqueue(struct mpq *, struct mbuf *);
struct mbuf	*mpq_dethread(struct mpq *);
struct mbuf	*mpq_dequeue(struct mpq *);
void		 mpq_done(struct mbuf *m);

#define MPQ_LEN 1024
#define MPQ_WORKER(fn_name, parallel_fn, serial_fn)			\
void fn_name(void *_mpq) {						\
	struct mbuf *m;							\
	struct mpq *mpq = _mpq;						\
	while ((m = mpq_dethread(mpq)) != NULL) {			\
		parallel_fn(m);						\
		mpq_threaddone(mpq, m);				\
	}								\
	if (mpq_serialize_try_enter(mpq) == 0) {			\
		while((m = mpq_dequeue(mpq)) != NULL) {			\
			serial_fn(m);					\
		}							\
		mpq_serialize_leave(mpq);				\
	}								\
}									\

#endif /* _MPQ_H_ */