• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "../include/ievent.h"
17 #include "db_errno.h"
18 #include "event_impl.h"
19 
20 namespace DistributedDB {
CreateEvent(EventTime timeout,int & errCode)21 IEvent *IEvent::CreateEvent(EventTime timeout, int &errCode)
22 {
23     if (timeout < 0) {
24         errCode = -E_INVALID_ARGS;
25         return nullptr;
26     }
27 
28     IEvent *event = new (std::nothrow) EventImpl(timeout);
29     if (event == nullptr) {
30         errCode = -E_OUT_OF_MEMORY;
31         return nullptr;
32     }
33     errCode = E_OK;
34     return event;
35 }
36 
CreateEvent(EventFd fd,EventsMask events,EventTime timeout,int & errCode)37 IEvent *IEvent::CreateEvent(EventFd fd, EventsMask events,
38     EventTime timeout, int &errCode)
39 {
40     errCode = -E_INVALID_ARGS;
41     if (!events) {
42         return nullptr;
43     }
44     if ((events & ET_TIMEOUT) && (timeout < 0)) {
45         return nullptr;
46     }
47     if (!(events & ET_TIMEOUT) && !fd.IsValid()) {
48         return nullptr;
49     }
50 
51     IEvent *event = new (std::nothrow) EventImpl(fd, events, timeout);
52     if (event == nullptr) {
53         errCode = -E_OUT_OF_MEMORY;
54         return nullptr;
55     }
56     errCode = E_OK;
57     return event;
58 }
59 } // namespace DistributedDB
60