/* SPDX-License-Identifier: GPL-2.0 */1/*2* An API to allow a function, that may fail, to be executed, and recover in a3* controlled manner.4*5* Copyright (C) 2019, Google LLC.6* Author: Brendan Higgins <[email protected]>7*/89#ifndef _KUNIT_TRY_CATCH_H10#define _KUNIT_TRY_CATCH_H1112#include <linux/types.h>1314typedef void (*kunit_try_catch_func_t)(void *);1516struct kunit;1718/**19* struct kunit_try_catch - provides a generic way to run code which might fail.20* @test: The test case that is currently being executed.21* @try_result: Contains any errno obtained while running test case.22* @try: The function, the test case, to attempt to run.23* @catch: The function called if @try bails out.24* @context: used to pass user data to the try and catch functions.25*26* kunit_try_catch provides a generic, architecture independent way to execute27* an arbitrary function of type kunit_try_catch_func_t which may bail out by28* calling kunit_try_catch_throw(). If kunit_try_catch_throw() is called, @try29* is stopped at the site of invocation and @catch is called.30*31* struct kunit_try_catch provides a generic interface for the functionality32* needed to implement kunit->abort() which in turn is needed for implementing33* assertions. Assertions allow stating a precondition for a test simplifying34* how test cases are written and presented.35*36* Assertions are like expectations, except they abort (call37* kunit_try_catch_throw()) when the specified condition is not met. This is38* useful when you look at a test case as a logical statement about some piece39* of code, where assertions are the premises for the test case, and the40* conclusion is a set of predicates, rather expectations, that must all be41* true. If your premises are violated, it does not makes sense to continue.42*/43struct kunit_try_catch {44/* private: internal use only. */45struct kunit *test;46int try_result;47kunit_try_catch_func_t try;48kunit_try_catch_func_t catch;49unsigned long timeout;50void *context;51};5253void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context);5455void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch);5657static inline int kunit_try_catch_get_result(struct kunit_try_catch *try_catch)58{59return try_catch->try_result;60}6162#endif /* _KUNIT_TRY_CATCH_H */636465