simple_stack.hh

00001 // This file is a part of Aurelia.
00002 // Copyright (C) 2010  Valentin David
00003 // Copyright (C) 2010  University of Bergen
00004 //
00005 // This program is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 #ifndef __SIMPLE_STACK_HH
00019 # define __SIMPLE_STACK_HH
00020 
00021 # include <memory>
00022 
00023 namespace lockfree {
00024 
00025   template <typename Elt,
00026             typename Alloc = std::allocator<Elt>
00027             >
00028   class simple_stack {
00029   private:
00030     struct node {
00031       Elt e;
00032       node* next;
00033       node(Elt&& e, node* next): e(std::move(e)), next(next) {}
00034     };
00035     typedef typename Alloc::template rebind<node>::other node_allocator_t;
00036 
00037     node_allocator_t alloc;
00038     std::atomic<node*> head;
00039   public:
00040     simple_stack(Alloc alloc = Alloc()): alloc(alloc), head(NULL) {}
00041 
00042     struct wrapped {
00043     private:
00044       node_allocator_t& alloc;
00045       node* n;
00046     public:
00047       wrapped(node_allocator_t& alloc, node *n): alloc(alloc), n(n) {}
00048       bool end() const {
00049         return (n == NULL);
00050       }
00051       Elt operator*() const {
00052         return n->e;
00053       }
00054       ~wrapped() {
00055         if (n != NULL) {
00056           alloc.destroy(n);
00057           alloc.deallocate(n, 1);
00058         }
00059       }
00060     };
00061 
00062     void push(const Elt& e) {
00063       push(Elt(e));
00064     }
00065 
00066     void push(Elt&& e) {
00067       node* n = alloc.allocate(1);
00068       alloc.construct(n, std::move(e), (node*)NULL);
00069       node *h;
00070       do {
00071         h = head.load();
00072         n->next = h;
00073       } while (!head.compare_exchange_strong(h, n));
00074     }
00075 
00076     wrapped pop() {
00077       node* n;
00078       do {
00079         n = head.load();
00080         if (n == NULL)
00081           return wrapped(alloc, NULL);
00082       } while (!head.compare_exchange_strong(n, n->next));
00083       return wrapped(alloc, n);
00084     }
00085   };
00086 
00087 }
00088 
00089 #endif