/* Generated automatically by jlpp - do not edit. */ #include static jakelib::lang::String* jakelib2_strings[] = {null, null}; // "" static jchar chars_jakelib2_str_0[] = {0}; // " <= 0" static jchar chars_jakelib2_str_1[] = {32,60,61,32,48}; #line 1 "util/Queue.jlc" // -*- c++ -*- /* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: Queue.jlc,v 1.4 2003/09/27 08:10:29 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/util/Queue.h" #include "jakelib2/util/ArrayList.h" using namespace jakelib::lang; using namespace jakelib::util; JAKELIB_IMPLEMENT_CLASS("jakelib.util.Queue", Queue, Object) #ifndef WINOWS32 # include # include #endif /*****************************************************************************\ * Queue | *****************************************************************************/ Queue::Queue(int size) { if (size <= 0) { throw new IllegalArgumentException(JAKELIB_ONDEMAND(jakelib2_strings[0], new jakelib::lang::String(chars_jakelib2_str_0, 0, 0)) ->plus( size )->plus( JAKELIB_ONDEMAND(jakelib2_strings[1], new jakelib::lang::String(chars_jakelib2_str_1, 0, 5)) )->plus( JAKELIB_AT2("jakelib.util.Queue.Queue"))); } queue = (Object**) GC_MALLOC(size * sizeof(Object**)); this->size = size; count = 0; readIndex = 0; writeIndex = 0; sync = new Semaphore(); readSem = new Semaphore(0); writeSem = new Semaphore(size); } /*****************************************************************************\ * ~Queue | *****************************************************************************/ Queue::~Queue() { GC_FREE(queue); delete sync; delete readSem; delete writeSem; } /*****************************************************************************\ * put | *****************************************************************************/ void Queue::put(Object* obj) { writeSem->p(); sync->p(); queue[writeIndex] = obj; writeIndex ++; writeIndex %= size; count ++; sync->v(); readSem->v(); } /*****************************************************************************\ * get | *****************************************************************************/ Object* Queue::get() { Object* obj; readSem->p(); sync->p(); obj = queue[readIndex]; queue[readIndex] = null; readIndex ++; readIndex %= size; count --; sync->v(); writeSem->v(); return obj; } /*****************************************************************************\ * isEmpty | *****************************************************************************/ jboolean Queue::isEmpty() { sync->p(); jboolean ret = (count == 0); sync->v(); return ret; } /*****************************************************************************\ * isFull | *****************************************************************************/ jboolean Queue::isFull() { sync->p(); jboolean ret = (count == size); sync->v(); return ret; } /*****************************************************************************\ * length | *****************************************************************************/ int Queue::length() { return size; } /*****************************************************************************\ * entries | *****************************************************************************/ int Queue::entries() { return count; } /*****************************************************************************\ * extractEntries | *****************************************************************************/ ArrayList* Queue::extractEntries() { ArrayList* list = new ArrayList(count); sync->p(); for (int idx = 0; idx < count; idx++) { list->add(queue[readIndex + idx]); } sync->v(); return list; }