1*5f3e0620SBrendan Higgins /* SPDX-License-Identifier: GPL-2.0 */ 2*5f3e0620SBrendan Higgins /* 3*5f3e0620SBrendan Higgins * An API to allow a function, that may fail, to be executed, and recover in a 4*5f3e0620SBrendan Higgins * controlled manner. 5*5f3e0620SBrendan Higgins * 6*5f3e0620SBrendan Higgins * Copyright (C) 2019, Google LLC. 7*5f3e0620SBrendan Higgins * Author: Brendan Higgins <brendanhiggins@google.com> 8*5f3e0620SBrendan Higgins */ 9*5f3e0620SBrendan Higgins 10*5f3e0620SBrendan Higgins #ifndef _KUNIT_TRY_CATCH_H 11*5f3e0620SBrendan Higgins #define _KUNIT_TRY_CATCH_H 12*5f3e0620SBrendan Higgins 13*5f3e0620SBrendan Higgins #include <linux/types.h> 14*5f3e0620SBrendan Higgins 15*5f3e0620SBrendan Higgins typedef void (*kunit_try_catch_func_t)(void *); 16*5f3e0620SBrendan Higgins 17*5f3e0620SBrendan Higgins struct completion; 18*5f3e0620SBrendan Higgins struct kunit; 19*5f3e0620SBrendan Higgins 20*5f3e0620SBrendan Higgins /** 21*5f3e0620SBrendan Higgins * struct kunit_try_catch - provides a generic way to run code which might fail. 22*5f3e0620SBrendan Higgins * @test: The test case that is currently being executed. 23*5f3e0620SBrendan Higgins * @try_completion: Completion that the control thread waits on while test runs. 24*5f3e0620SBrendan Higgins * @try_result: Contains any errno obtained while running test case. 25*5f3e0620SBrendan Higgins * @try: The function, the test case, to attempt to run. 26*5f3e0620SBrendan Higgins * @catch: The function called if @try bails out. 27*5f3e0620SBrendan Higgins * @context: used to pass user data to the try and catch functions. 28*5f3e0620SBrendan Higgins * 29*5f3e0620SBrendan Higgins * kunit_try_catch provides a generic, architecture independent way to execute 30*5f3e0620SBrendan Higgins * an arbitrary function of type kunit_try_catch_func_t which may bail out by 31*5f3e0620SBrendan Higgins * calling kunit_try_catch_throw(). If kunit_try_catch_throw() is called, @try 32*5f3e0620SBrendan Higgins * is stopped at the site of invocation and @catch is called. 33*5f3e0620SBrendan Higgins * 34*5f3e0620SBrendan Higgins * struct kunit_try_catch provides a generic interface for the functionality 35*5f3e0620SBrendan Higgins * needed to implement kunit->abort() which in turn is needed for implementing 36*5f3e0620SBrendan Higgins * assertions. Assertions allow stating a precondition for a test simplifying 37*5f3e0620SBrendan Higgins * how test cases are written and presented. 38*5f3e0620SBrendan Higgins * 39*5f3e0620SBrendan Higgins * Assertions are like expectations, except they abort (call 40*5f3e0620SBrendan Higgins * kunit_try_catch_throw()) when the specified condition is not met. This is 41*5f3e0620SBrendan Higgins * useful when you look at a test case as a logical statement about some piece 42*5f3e0620SBrendan Higgins * of code, where assertions are the premises for the test case, and the 43*5f3e0620SBrendan Higgins * conclusion is a set of predicates, rather expectations, that must all be 44*5f3e0620SBrendan Higgins * true. If your premises are violated, it does not makes sense to continue. 45*5f3e0620SBrendan Higgins */ 46*5f3e0620SBrendan Higgins struct kunit_try_catch { 47*5f3e0620SBrendan Higgins /* private: internal use only. */ 48*5f3e0620SBrendan Higgins struct kunit *test; 49*5f3e0620SBrendan Higgins struct completion *try_completion; 50*5f3e0620SBrendan Higgins int try_result; 51*5f3e0620SBrendan Higgins kunit_try_catch_func_t try; 52*5f3e0620SBrendan Higgins kunit_try_catch_func_t catch; 53*5f3e0620SBrendan Higgins void *context; 54*5f3e0620SBrendan Higgins }; 55*5f3e0620SBrendan Higgins 56*5f3e0620SBrendan Higgins void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context); 57*5f3e0620SBrendan Higgins 58*5f3e0620SBrendan Higgins void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch); 59*5f3e0620SBrendan Higgins 60*5f3e0620SBrendan Higgins static inline int kunit_try_catch_get_result(struct kunit_try_catch *try_catch) 61*5f3e0620SBrendan Higgins { 62*5f3e0620SBrendan Higgins return try_catch->try_result; 63*5f3e0620SBrendan Higgins } 64*5f3e0620SBrendan Higgins 65*5f3e0620SBrendan Higgins #endif /* _KUNIT_TRY_CATCH_H */ 66