Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame^] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #include "task.h" |
| 23 | #include "scheduler.h" |
| 24 | |
| 25 | void |
| 26 | eventCallback(evutil_socket_t fd, short what, void *arg) |
| 27 | { |
| 28 | Task *task = static_cast<Task *>(arg); |
| 29 | task->run(); |
| 30 | task = NULL; |
| 31 | } |
| 32 | |
| 33 | Task::Task(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler) |
| 34 | : m_callback(callback) |
| 35 | , m_tag(tag) |
| 36 | , m_scheduler(scheduler) |
| 37 | , m_invoked(false) |
| 38 | , m_event(NULL) |
| 39 | , m_tv(NULL) |
| 40 | { |
| 41 | m_event = evtimer_new(scheduler->base(), eventCallback, this); |
| 42 | m_tv = new timeval; |
| 43 | } |
| 44 | |
| 45 | Task::~Task() |
| 46 | { |
| 47 | if (m_event != NULL) |
| 48 | { |
| 49 | event_free(m_event); |
| 50 | m_event = NULL; |
| 51 | } |
| 52 | |
| 53 | if (m_tv != NULL) |
| 54 | { |
| 55 | delete m_tv; |
| 56 | m_tv = NULL; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | Task::setTv(double delay) |
| 62 | { |
| 63 | double intPart, fraction; |
| 64 | fraction = modf(abs(delay), &intPart); |
| 65 | m_tv->tv_sec = static_cast<int>(intPart); |
| 66 | m_tv->tv_usec = static_cast<int>((fraction * 1000000)); |
| 67 | } |